Python-http请求

Python-http请求,第1张

概述#-*-coding:utf-8-*-importtimeimportrequestsimporthttpximportaiohttpimportasynciourl='https://www.baidu.com/'######################################时间装饰器###################################################defwrapper(fu
# -*- Coding: utf-8 -*-import timeimport requestsimport httpximport aiohttpimport asynciourl = '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# ##################################### 同步请求 ###################################################@wrapperdef requests_sync_test(count=100):    session = requests.Session()    for _ in range(count):        res = session.get(url=url)        print(res.Json())@wrapperdef 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)@wrapperasync 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)@wrapperasync 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

总结

以上是内存溢出为你收集整理的Python-http请求全部内容,希望文章能够帮你解决Python-http请求所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1186080.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存