import asyncio
import pycurl
from io import BytesIO
import json
def fetch_api(url, method, header=None, data=None):
"""
url: 獲取api的url
method: 請求方法
header: 請求頭
data: 請求參數
"""
if method == "get":
_Curl = pycurl.Curl()
buf = BytesIO()
_Curl.setopt(pycurl.WRITEFUNCTION,buf.write)
_Curl.setopt(pycurl.URL, url)
_Curl.setopt(pycurl.HTTPHEADER, header)
_Curl.perform()
result = buf.getvalue().decode("utf-8")
http_code = _Curl.getinfo(_Curl.HTTP_CODE)
dns_time = _Curl.getinfo(_Curl.NAMELOOKUP_TIME) * 1000 # DNS解析時間
connect_time = _Curl.getinfo(_Curl.CONNECT_TIME) * 1000 # 建連時間
pretransafe_time = _Curl.getinfo(_Curl.PRETRANSFER_TIME) * 1000 # 連接上後到開始傳輸時的時間
starttransafe_time =_Curl.getinfo(_Curl.STARTTRANSFER_TIME) * 1000 # 接收到第一個字節的時間
redirect_time = _Curl.getinfo(_Curl.REDIRECT_TIME) * 1000 # 重定向時間
total_time = _Curl.getinfo(_Curl.TOTAL_TIME) * 1000 # 請求總時間
download_size = _Curl.getinfo(_Curl.SIZE_DOWNLOAD) # 下載數據包大小
download_speed = _Curl.getinfo(_Curl.SPEED_DOWNLOAD) # 下載速度
buf.close()
print(result)
print(http_code,dns_time,connect_time,pretransafe_time,starttransafe_time,redirect_time,total_time,download_size,download_speed)
_Curl.close()
else:
_Curl = pycurl.Curl()
_Curl.setopt(pycurl.HTTPHEADER, header)
buf = BytesIO()
_Curl.setopt(pycurl.WRITEFUNCTION, buf.write)
_Curl.setopt(pycurl.URL, url)
_Curl.setopt(_Curl.POSTFIELDS, data)
_Curl.perform()
result = buf.getvalue().decode("utf-8")
http_code = _Curl.getinfo(_Curl.HTTP_CODE)
dns_time = _Curl.getinfo(_Curl.NAMELOOKUP_TIME) * 1000 # DNS解析時間
connect_time = _Curl.getinfo(_Curl.CONNECT_TIME) * 1000 # 建連時間
pretransafe_time = _Curl.getinfo(_Curl.PRETRANSFER_TIME) * 1000 # 連接上後到開始傳輸時的時間
starttransafe_time = _Curl.getinfo(_Curl.STARTTRANSFER_TIME) * 1000 # 接收到第一個字節的時間
redirect_time = _Curl.getinfo(_Curl.REDIRECT_TIME) * 1000 # 重定向時間
total_time = _Curl.getinfo(_Curl.TOTAL_TIME) * 1000 # 請求總時間 毫秒
download_size = _Curl.getinfo(_Curl.SIZE_DOWNLOAD) # 下載數據包大小
download_speed = _Curl.getinfo(_Curl.SPEED_DOWNLOAD) # 下載速度
buf.close()
print(result)
print(http_code, dns_time, connect_time, pretransafe_time, starttransafe_time, redirect_time, total_time,
download_size, download_speed)
_Curl.close()
"""
get請求測試
header = [
'Content-Type:application/json; charset=utf-8'
]
fetch_api('http://xxxx', 'get', header)
post請求測試
header = [
'Content-Type:application/json; charset=utf-8'
]
data = {"userid":"%s","nickname":"zxpp","sex":"1","birthday":"2015-01-01"}
json_data = json.dumps(data)
fetch_api('http://xxxx', 'post', header=header,data=json_data)
"""
@asyncio.coroutine
def fetch_async(func, url, method, header=None, data=None):
loop = asyncio.get_event_loop()
future = loop.run_in_executor(None, func, url, method,header,data)
#response = yield from future # 因爲 func 函數未返回數據,所以可以不用返回值
yield from future
header = [
'Content-Type:application/json; charset=utf-8'
]
data = {"userid":"%s","nickname":"zxpp","sex":"1","birthday":"2015-01-01"}
json_data = json.dumps(data)
tasks = [
fetch_async(fetch_api, url='http://xxxx', method='get',header=header),
fetch_async(fetch_api, url='http://xxxx', method='post', header=header,data=json_data)
]
loop = asyncio.get_event_loop()
results = loop.run_until_complete(asyncio.gather(*tasks))
loop.close()