Python - HTTPX
入门案例
num = 5
semaphore = asyncio.Semaphore(value=num)
async def request_model(model, client):
async with semaphore:
try:
body = {
"searchKeyWord": model
}
resp = await client.post(searchRecommend_url, json=body)
if resp.status_code == 200:
searchRecommend_json = resp.json()
codeOrNumRecommendList = searchRecommend_json['data']['codeOrNumRecommendList']
if len(codeOrNumRecommendList) == 0:
logger.error(f'searchRecommend => model:{model} 返回 codeOrNumRecommendList 为空')
return
client.cookies.clear()
productCodeAccessId = codeOrNumRecommendList[0]['productCodeAccessId']
attrSelectionList = []
for item in codeOrNumRecommendList[0]['paramInfoList']:
name = item['paramName']
value = item['paramValue']
attrSelectionList.append({"name": name, "value": value})
product_code = codeOrNumRecommendList[0]['productCode']
requestModel_data = {
"attrInfo": attrSelectionList,
"isDownloadRequest": 0,
"modelType": "glb",
"productCode": product_code,
"productCodeAccessId": productCodeAccessId,
"productModel": model
}
requestModel_resp = await client.post(requestModel_url, json=requestModel_data)
client.cookies.clear()
if requestModel_resp.status_code == 200:
logger.info(f'请求model:{model} 建模完毕')
else:
logger.info(
f'请求建模{requestModel_url}失败, 返回信息:{await requestModel_resp.text()},请求信息:{requestModel_resp.request_info}')
else:
logger.error(f'searchRecommend_url failure,返回信息:{resp.text},请求信息:{resp.request.url}')
except httpx._exceptions.RequestError as err:
logger.exception(err)
async def req_main():
async with httpx.AsyncClient(headers=fa_header) as clinet:
with open('c.txt', 'r', encoding='utf-8') as f:
models = f.readlines()
tasks = [request_model(model.rstrip(),clinet) for model in models]
await asyncio.gather(*tasks)
if __name__ == '__main__':
loop = asyncio.new_event_loop()
loop.run_until_complete(req_main())
HTTPX 和 aiohttp 的区别
通过入门案例可以总结:
- httpx 在获取响应内容时不用像 aiohttp 一样 在resp.json() 前追加 await
aiohttp json 函数签名:
async def json(
self,
*,
encoding: Optional[str] = None,
loads: JSONDecoder = DEFAULT_JSON_DECODER,
content_type: Optional[str] = "application/json",
) -> Any:
httpx json 函数签名:
def json(self, **kwargs: typing.Any) -> typing.Any:
- httpx:在清空clent 的cookei 时可以用 : client.cookies.clear()
https://zhuanlan.zhihu.com/p/659975764
省流:
httpx:api简单易用,轻量,如果只是请求服务器数据,使用httpx即可
aiohttp: 比较重量,可以开发Web 服务器
学习资料:
https://www.python-httpx.org/exceptions/
https://blog.csdn.net/chinesehuazhou2/article/details/129095625
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/17415958.html