python请求
简介
1. 封装相关API依赖(同步|异步)调用服务端请求
2. 相关依赖
http.client
urllib
requests
aiohttp
httpx
实例
依赖
python3.7+
aiohttp==3.8.5
aiosignal==1.3.1
anyio==3.7.1
async-timeout==4.0.3
asynctest==0.13.0
attrs==23.1.0
certifi==2023.7.22
charset-normalizer==3.2.0
exceptiongroup==1.1.3
frozenlist==1.3.3
h11==0.14.0
httpcore==0.17.3
httpx==0.24.1
idna==3.4
importlib-metadata==6.7.0
multidict==6.0.4
requests==2.31.0
sniffio==1.3.0
typing_extensions==4.7.1
urllib3==2.0.4
yarl==1.9.2
zipp==3.15.0
服务端
'''
pip install flask
'''
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route("/", methods=["POST", "GET"])
def index():
return jsonify({"code": 0,
"msg": "ok",
"data": {
"args": request.args,
"form": request.form,
"json": request.get_json(),
}
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=6666, debug=False)
客户端
http.client
import time
from functools import wraps
def cal(func):
@wraps(func)
def inner(*args, **kwargs):
st = time.time()
stl = func(*args, **kwargs)
et = round((time.time() - st), 4) * 1000
print(f"Func:{func.__name__};总请求数:{len(stl)};成功:{len([s for s in stl if s == 200])};总耗时:{et}ms")
return inner
@cal
def my_httpclient(count=1):
"""
内置http模块客户端端请求
:param count:
:return:
"""
import http.client
import json
stl = []
while count > 0:
con = http.client.HTTPConnection(host="localhost", port=6666)
con.request(method="GET",
url="/?name=args" + str(count),
body=None,
headers={})
res = con.getresponse()
stl.append(res.status)
con.close()
count -= 1
return stl
urllib
import time
from functools import wraps
def cal(func):
@wraps(func)
def inner(*args, **kwargs):
st = time.time()
stl = func(*args, **kwargs)
et = round((time.time() - st), 4) * 1000
print(f"Func:{func.__name__};总请求数:{len(stl)};成功:{len([s for s in stl if s == 200])};总耗时:{et}ms")
return inner
@cal
def my_urllib(count=1):
"""
urllib
:param count:
:return:
"""
stl = []
import urllib.request
import json
while count > 0:
req = urllib.request.Request(url="http://localhost:6666?name=args" + str(count),
data=None,
headers={},
method="GET")
with urllib.request.urlopen(req) as rq:
stl.append(rq.status)
count -= 1
return stl
requests
pip install requests -i http://mirrors.cloud.tencent.com/pypi/simple --trusted-host mirrors.cloud.tencent.com
ps: 可选参数
--proxy=http(s)://ip:port
import time
from functools import wraps
def cal(func):
@wraps(func)
def inner(*args, **kwargs):
st = time.time()
stl = func(*args, **kwargs)
et = round((time.time() - st), 4) * 1000
print(f"Func:{func.__name__};总请求数:{len(stl)};成功:{len([s for s in stl if s == 200])};总耗时:{et}ms")
return inner
@cal
def my_requests(count=1):
"""
requests支持同步请求
pip install requests --proxy=http(s)://ip:port
:param count:
:return:
"""
stl = []
import requests
ss = requests.session()
while count > 0:
rs = ss.get(url="http://localhost:6666?name=args" + str(count),
data=None,
headers={})
stl.append(rs.status_code)
count -= 1
return stl
aiohttp
pip install aiohttp
async def my_aiohttp(count=1):
"""
aiohttp支持异步请求
pip install aiohttp
:param count:
:return:
"""
stl = []
st = time.time()
import aiohttp
async with aiohttp.ClientSession() as ss:
while count > 0:
async with ss.get(
url="http://localhost:6666?name=args" + str(count),
) as rs:
data, code = await rs.json(), rs.status
stl.append(code)
count -= 1
et = round((time.time() - st), 4) * 1000
print(f"Func:{my_aiohttp.__name__};总请求数:{len(stl)};成功:{len([s for s in stl if s == 200])};总耗时:{et}ms")
import asyncio
asyncio.run(my_aiohttp(1))
httpx-sync
pip install httpx
import time
from functools import wraps
def cal(func):
@wraps(func)
def inner(*args, **kwargs):
st = time.time()
stl = func(*args, **kwargs)
et = round((time.time() - st), 4) * 1000
print(f"Func:{func.__name__};总请求数:{len(stl)};成功:{len([s for s in stl if s == 200])};总耗时:{et}ms")
return inner
@cal
def my_httpx_sync(count=1):
"""
pip install httpx
httpx 支持同步和异步请求
测试时关闭代理
:param count:
:return:
"""
import httpx
stl = []
with httpx.Client() as c:
while count > 0:
rs = c.get(url="http://localhost:6666",
params={"name": "args" + str(count)},
timeout=3,
)
stl.append(rs.status_code)
count -= 1
return stl
httpx-async
async def async_httpx_task(c, i):
"""
httpx 异步请求任务
:param c:
:param i:
:return:
"""
rs = await c.get(url="http://localhost:6666",
params={"name": "args" + str(i)})
return rs.status_code
async def my_httpx_async(count=1):
"""
httpx异步请求入口函数
:param count:
:return:
"""
st = time.time()
import httpx, asyncio
async with httpx.AsyncClient(timeout=30) as c:
stl = await asyncio.gather(*[async_httpx_task(c, i) for i in range(count)])
et = round((time.time() - st), 4) * 1000
print(f"Func:{my_httpx_async.__name__};总请求数:{len(stl)};成功:{len([s for s in stl if s == 200])};总耗时:{et}ms")
import asyncio
asyncio.run(my_httpx_async(n))

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?