python同步/异步请求

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

服务端

# -*- coding:utf-8 -*-
'''
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)
        
        # 1. 查询字符串?a=a
        con.request(method="GET",
                    url="/?name=args" + str(count),
                    body=None,
                    headers={})
        
        # # 2. FORM表单
        # data = json.dumps({"name": "form"})
        # con.request(method="GET", url="/?name=http", body=data, headers={"Content-Type": "application/x-www-form-urlencoded"})
        
        # # 3. JSON数据
        # data = json.dumps({"name": "json"})
        # con.request(method="GET", url="/?name=args", body=data, headers={"Content-Type": "application/json"})
        res = con.getresponse()
        # print(f"status:{res.status};data:{res.read().decode()}")
        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:
        # 1. 查询字符?a=a
        req = urllib.request.Request(url="http://localhost:6666?name=args" + str(count),
                                     data=None,
                                     headers={},
                                     method="GET")
        
        # # 2. FORM表单
        # data = json.dumps({"name": "form"}).encode()
        # req = urllib.request.Request(url="http://localhost:6666",
        #                              data=data,
        #                              headers={"Content-Type": "application/x-www-form-urlencoded"},
        #                              method="GET"
        #                              )
        
        # # 3. JSON
        # data = json.dumps({"name": "json"}).encode()
        # req = urllib.request.Request(url="http://localhost:6666",
        #                              data=data,
        #                              headers={"Content-Type": "application/json"},
        #                              method="GET"
        #                              )
        
        with urllib.request.urlopen(req) as rq:
            # print(f"status:{rq.status};data:{rq.read().decode()}")
            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 = requests.get(url="http://localhost:6666?name=args",
        #                   data=None,
        #                   headers={})
        # 1. 查询字符串
        rs = ss.get(url="http://localhost:6666?name=args" + str(count),
                    data=None,
                    headers={})
        
        # # 2. FORM表单
        # rs = requests.get(url="http://localhost:6666",
        #                   data={"name": "form"},
        #                   headers={})
        
        # # 3. JSON
        # rs = requests.get(url="http://localhost:6666",
        #                   json={"name": "json"},
        #                   headers={})
        
        # print(f"status:{rs.status_code};data:{rs.json()}")
        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),
                    # json={"name": "json"},  # JSON
                    # data={"name": "form"},  # FORM表单
                              ) as rs:
                data, code = await rs.json(), rs.status
                # print(f"status:{code};data:{data}")
                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:
            # 1.查询字符串
            rs = c.get(url="http://localhost:6666",
                       params={"name": "args" + str(count)},
                       timeout=3,
                       )
            
            # # 2. FORM-不能是GET请求
            # rs = c.post(url="http://localhost:6666",
            #             data={"name": "form"},
            #             timeout=3,
            #            )
            
            # # 3. JSON-不能是GET请求
            # rs = c.post(url="http://localhost:6666",
            #             json={"name": "json"},
            #             timeout=3,
            #             )
            
            # print(f"status:{rs.status_code};data:{rs.json()}")
            
            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)})
    # print(f"status:{rs.status_code};data:{rs.json()}")
    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))


posted @   爱编程_喵  阅读(460)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
jQuery火箭图标返回顶部代码

jQuery火箭图标返回顶部代码

滚动滑动条后,查看右下角查看效果。很炫哦!!

适用浏览器:IE8、360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗.

点击右上角即可分享
微信分享提示