zoukankan      html  css  js  c++  java
  • Python-http请求

    # -*- coding: utf-8 -*-
    
    import time
    import requests
    import httpx
    import aiohttp
    import asyncio
    
    
    url = 'https://www.baidu.com/'
    
    
    # ##################################### 时间装饰器 ###################################################
    
    
    def wrapper(func):
        def inner(*args, **kwargs):
            start_time = time.time()
            res = func(*args, **kwargs)
            print(time.time() - start_time)
            return res
        return inner
    
    # ##################################### 同步请求 ###################################################
    
    
    @wrapper
    def requests_sync_test(count=100):
        session = requests.Session()
        for _ in range(count):
            res = session.get(url=url)
            print(res.json())
    
    
    @wrapper
    def http_x_sync_test(count=100):
        for _ in range(count):
            res = httpx.get(url=url)
            print(res.json())
    
    
    # ##################################### 异步请求 ###################################################
    
    
    async def request(client):
        resp = await client.get(url=url)
        result = resp.json()
        print(result)
    
    
    @wrapper
    async def http_x_async_test(count=100):
        async with httpx.AsyncClient() as client:
            task_list = []
            for _ in range(count):
                task = asyncio.create_task(
                    request(client=client)
                )
                task_list.append(task)
            await asyncio.gather(*task_list)
    
    
    @wrapper
    async def aio_http_async_text(count=100):
        """废弃|太慢"""
        async with aiohttp.ClientSession() as client:
            for _ in range(count):
                res = await client.get(url=url)
                res_json = await res.json()
                print(res_json)
    
    
    if __name__ == '__main__':
        requests_sync_test(count=10)
        # asyncio.run(http_x_async_test(10))
        # asyncio.run(aio_http_async_text(10))

    参考:https://www.pythonf.cn/read/36407

  • 相关阅读:
    IO模型详解
    Java中的CAS实现原理
    深入理解幂等性
    区块链基本原理入门
    通俗易懂讲解IO模型
    java高级特性(4)--枚举
    static、final和finalize详解
    锁(3)-- DB锁
    浅析项目中的并发
    分布式(1)-- 分布式锁
  • 原文地址:https://www.cnblogs.com/52-qq/p/14143899.html
Copyright © 2011-2022 走看看