Pytho——naiohttp的简单使用

news2024/10/6 6:04:15

1.aiohttp的简单使用(配合asyncio模块)

import asyncio,aiohttp

async def fetch_async(url):
    print(url)
    async with aiohttp.request("GET",url) as r:
        reponse = await r.text(encoding="utf-8")  #或者直接await r.read()不编码,直接读取,适合于图像等无法编码文件
        print(reponse)

tasks = [fetch_async('http://www.baidu.com/'), fetch_async('http://www.chouti.com/')]

event_loop = asyncio.get_event_loop()
results = event_loop.run_until_complete(asyncio.gather(*tasks))
event_loop.close()

2.发起一个session请求

import asyncio,aiohttp

async def fetch_async(url):
    print(url)
    async with aiohttp.ClientSession() as session:  #协程嵌套,只需要处理最外层协程即可fetch_async
        async with session.get(url) as resp:
            print(resp.status)
            print(await resp.text())  #因为这里使用到了await关键字,实现异步,所有他上面的函数体需要声明为异步async

tasks = [fetch_async('http://www.baidu.com/'), fetch_async('http://www.cnblogs.com/ssyfj/')]

event_loop = asyncio.get_event_loop()
results = event_loop.run_until_complete(asyncio.gather(*tasks))
event_loop.close()

除了上面的get方法外,会话还支持post,put,delete....等

session.put('http://httpbin.org/put', data=b'data')
session.delete('http://httpbin.org/delete')
session.head('http://httpbin.org/get')
session.options('http://httpbin.org/get')
session.patch('http://httpbin.org/patch', data=b'data')

不要为每次的连接都创建一次session,一般情况下只需要创建一个session,然后使用这个session执行所有的请求。

每个session对象,内部包含了一个连接池,并且将会保持连接和连接复用(默认开启)可以加快整体的性能。

3.在url中传递参数(其实与requests模块使用大致相同)

只需要将参数字典,传入params参数中即可

import asyncio,aiohttp

async def func1(url,params):
    async with aiohttp.ClientSession() as session:
        async with session.get(url,params=params) as r:
            print(r.url)
            print(await r.read())

tasks = [func1('https://www.ckook.com/forum.php',{"gid":6}),]

event_loop = asyncio.get_event_loop()
results = event_loop.run_until_complete(asyncio.gather(*tasks))
event_loop.close()

4.获取响应内容(由于获取响应内容是一个阻塞耗时过程,所以我们使用await实现协程切换)

(1)使用text()方法

async def func1(url,params):
    async with aiohttp.ClientSession() as session:
        async with session.get(url,params=params) as r:
            print(r.url)
            print(r.charset)  #查看默认编码为utf-8
            print(await r.text())  #不编码,则是使用默认编码  使用encoding指定编码​​​​​​​

(2)使用read()方法,不进行编码,为字节形式

async def func1(url,params):
    async with aiohttp.ClientSession() as session:
        async with session.get(url,params=params) as r:
            print(r.url)
            print(await r.read())

(3)注意:text(),read()方法是把整个响应体读入内存,如果你是获取大量的数据,请考虑使用”字节流“(StreamResponse)

5.特殊响应内容json(和上面一样)

async def func1(url,params):
    async with aiohttp.ClientSession() as session:
        async with session.get(url,params=params) as r:
            print(r.url)
            print(r.charset)
            print(await r.json())  #可以设置编码,设置处理函数​​​​​​​​​​​​​​

6.字节流形式获取数据(不像text,read一次获取所有数据)

注意:我们获取的session.get()是Response对象,他继承于StreamResponse

async def func1(url,params):
    async with aiohttp.ClientSession() as session:
        async with session.get(url,params=params) as r:
            print(await r.content.read(10))    #读取前10字节

下面字节流形式读取数据,保存文件

async def func1(url,params,filename):
    async with aiohttp.ClientSession() as session:
        async with session.get(url,params=params) as r:
            with open(filename,"wb") as fp:
                while True:
                    chunk = await r.content.read(10)
                    if not chunk:
                        break
                    fp.write(chunk)

tasks = [func1('https://www.ckook.com/forum.php',{"gid":6},"1.html"),]

注意:

async with session.get(url,params=params) as r:  #异步上下文管理器
with open(filename,"wb") as fp:  #普通上下文管理器

两者的区别:

在于异步上下文管理器中定义了

__aenter__和__aexit__方法

异步上下文管理器指的是在enterexit方法处能够暂停执行的上下文管理器

 为了实现这样的功能,需要加入两个新的方法:__aenter__ 和__aexit__。这两个方法都要返回一个 awaitable类型的值。

7.自定义请求头(和requests一样)

async def func1(url,params,filename):
    async with aiohttp.ClientSession() as session:
        headers = {'Content-Type':'text/html; charset=utf-8'}
        async with session.get(url,params=params,headers=headers) as r:
            with open(filename,"wb") as fp:
                while True:
                    chunk = await r.content.read(10)
                    if not chunk:
                        break
                    fp.write(chunk)

8.自定义cookie

注意:对于自定义cookie,我们需要设置在ClientSession(cookies=自定义cookie字典),而不是session.get()中

class ClientSession:

    def __init__(self, *, connector=None, loop=None, cookies=None,
                 headers=None, skip_auto_headers=None,
                 auth=None, json_serialize=json.dumps,
                 request_class=ClientRequest, response_class=ClientResponse,
                 ws_response_class=ClientWebSocketResponse,
                 version=http.HttpVersion11,
                 cookie_jar=None, connector_owner=True, raise_for_status=False,
                 read_timeout=sentinel, conn_timeout=None,
                 timeout=sentinel,
                 auto_decompress=True, trust_env=False,
                 trace_configs=None):

使用:

cookies = {'cookies_are': 'working'}
async with ClientSession(cookies=cookies) as session:

9.获取当前访问网站的cookie

async with session.get(url) as resp:
    print(resp.cookies)

10.获取网站的响应状态码

async with session.get(url) as resp:
    print(resp.status)

11.查看响应头

resp.headers 来查看响应头,得到的值类型是一个dict
resp.raw_headers  查看原生的响应头,字节类型

12.查看重定向的响应头(我们此时已经到了新的网址,向之前的网址查看)

resp.history  #查看被重定向之前的响应头

13.超时处理

默认的IO操作都有5分钟的响应时间 我们可以通过 timeout 进行重写:

async with session.get('https://github.com', timeout=60) as r:
    ...

如果 timeout=None 或者 timeout=0 将不进行超时检查,也就是不限时长。

14.ClientSession 用于在多个连接之间(同一网站)共享cookie,请求头等

async def func1():
    cookies = {'my_cookie': "my_value"}
    async with aiohttp.ClientSession(cookies=cookies) as session:
        async with session.get("https://segmentfault.com/q/1010000007987098") as r:
            print(session.cookie_jar.filter_cookies("https://segmentfault.com"))
        async with session.get("https://segmentfault.com/hottest") as rp:
            print(session.cookie_jar.filter_cookies("https://segmentfault.com"))
Set-Cookie: PHPSESSID=web2~d8grl63pegika2202s8184ct2q
Set-Cookie: my_cookie=my_value
Set-Cookie: PHPSESSID=web2~d8grl63pegika2202s8184ct2q
Set-Cookie: my_cookie=my_value

我们最好使用session.cookie_jar.filter_cookies()获取网站cookie,不同于requests模块,虽然我们可以使用rp.cookies有可能获取到cookie,但似乎并未获取到所有的cookies。


​​​​​​​

async def func1():
    cookies = {'my_cookie': "my_value"}
    async with aiohttp.ClientSession(cookies=cookies) as session:
        async with session.get("https://segmentfault.com/q/1010000007987098") as rp:
            print(session.cookie_jar.filter_cookies("https://segmentfault.com"))
            print(rp.cookies)  #Set-Cookie: PHPSESSID=web2~jh3ouqoabvr4e72f87vtherkp6; Domain=segmentfault.com; Path=/  #首次访问会获取网站设置的cookie
        async with session.get("https://segmentfault.com/hottest") as rp:
            print(session.cookie_jar.filter_cookies("https://segmentfault.com"))
            print(rp.cookies)  #为空,服务端未设置cookie
        async with session.get("https://segmentfault.com/newest") as rp:
            print(session.cookie_jar.filter_cookies("https://segmentfault.com"))
            print(rp.cookies)  #为空,服务端未设置cookie

总结:

当我们使用rp.cookie时,只会获取到当前url下设置的cookie,不会维护整站的cookie

而session.cookie_jar.filter_cookies("https://segmentfault.com")会一直保留这个网站的所有设置cookies,含有我们在会话时设置的cookie,并且会根据响应修改更新cookie。这个才是我们需要的

而我们设置cookie,也是需要在aiohttp.ClientSession(cookies=cookies)中设置

ClientSession 还支持 请求头,keep-alive连接和连接池(connection pooling)

15.cookie的安全性

默认ClientSession使用的是严格模式的 aiohttp.CookieJar. RFC 2109,明确的禁止接受url和ip地址产生的cookie,只能接受 DNS 解析IP产生的cookie。可以通过设置aiohttp.CookieJar 的 unsafe=True 来配置:

jar = aiohttp.CookieJar(unsafe=True)
session = aiohttp.ClientSession(cookie_jar=jar)

16.控制同时连接的数量(连接池)

TCPConnector维持链接池,限制并行连接的总量,当池满了,有请求退出再加入新请求

async def func1():
    cookies = {'my_cookie': "my_value"}
    conn = aiohttp.TCPConnector(limit=2)  #默认100,0表示无限
    async with aiohttp.ClientSession(cookies=cookies,connector=conn) as session:
        for i in range(7,35):
            url = "https://www.ckook.com/list-%s-1.html"%i
            async with session.get(url) as rp:
                print('---------------------------------')
                print(rp.status)

限制同时打开限制同时打开连接到同一端点的数量((host, port, is_ssl) 三的倍数),可以通过设置 limit_per_host 参数:

limit_per_host: 同一端点的最大连接数量。同一端点即(host, port, is_ssl)完全相同

conn = aiohttp.TCPConnector(limit_per_host=30)#默认是0

在协程下测试效果不明显

17.自定义域名解析地址

我们可以指定域名服务器的 IP 对我们提供的get或post的url进行解析:

from aiohttp.resolver import AsyncResolver
 
resolver = AsyncResolver(nameservers=["8.8.8.8", "8.8.4.4"])
conn = aiohttp.TCPConnector(resolver=resolver)

18.设置代理

aiohttp支持使用代理来访问网页:

async with aiohttp.ClientSession() as session:
    async with session.get("http://python.org",
                           proxy="http://some.proxy.com") as resp:
        print(resp.status)

当然也支持需要授权的页面:

async with aiohttp.ClientSession() as session:
    proxy_auth = aiohttp.BasicAuth('user', 'pass')  #用户,密码
    async with session.get("http://python.org",
                           proxy="http://some.proxy.com",
                           proxy_auth=proxy_auth) as resp:
        print(resp.status)

或者通过这种方式来验证授权:

session.get("http://python.org",
            proxy="http://user:pass@some.proxy.com")

19.post传递数据的方法

(1)模拟表单

payload = {'key1': 'value1', 'key2': 'value2'}
async with session.post('http://httpbin.org/post',
                        data=payload) as resp:
    print(await resp.text())

注意:data=dict的方式post的数据将被转码,和form提交数据是一样的作用,如果你不想被转码,可以直接以字符串的形式 data=str 提交,这样就不会被转码。

(2)post json

payload = {'some': 'data'}
 
async with session.post(url, data=json.dumps(payload)) as resp:

其实json.dumps(payload)返回的也是一个字符串,只不过这个字符串可以被识别为json格式

(3)post 小文件

url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
 
await session.post(url, data=files)
url = 'http://httpbin.org/post'
data = FormData()
data.add_field('file',
               open('report.xls', 'rb'),
               filename='report.xls',
               content_type='application/vnd.ms-excel')
 
await session.post(url, data=data)

如果将文件对象设置为数据参数,aiohttp将自动以字节流的形式发送给服务器。

(4)post 大文件

aiohttp支持多种类型的文件以流媒体的形式上传,所以我们可以在文件未读入内存的情况下发送大文件。

@aiohttp.streamer
def file_sender(writer, file_name=None):
    with open(file_name, 'rb') as f:
        chunk = f.read(2**16)
        while chunk:
            yield from writer.write(chunk)
            chunk = f.read(2**16)
 
# Then you can use `file_sender` as a data provider:
 
async with session.post('http://httpbin.org/post',
                        data=file_sender(file_name='huge_file')) as resp:
    print(await resp.text())

(5)从一个url获取文件后,直接post给另一个url

r = await session.get('http://python.org')
await session.post('http://httpbin.org/post',data=r.content)

(6)post预压缩数据

在通过aiohttp发送前就已经压缩的数据, 调用压缩函数的函数名(通常是deflate 或 zlib)作为content-encoding的值:

async def my_coroutine(session, headers, my_data):
    data = zlib.compress(my_data)
    headers = {'Content-Encoding': 'deflate'}
    async with session.post('http://httpbin.org/post',
                            data=data,
                            headers=headers)
        pass

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/78212.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

我不谈ChatGPT

(1)数据有两个未经证实的传闻:1、客服问答:80%用户问的问题都是那20%常见问题,但是就是这样,占用了客服人员80%的工作量和工作时间2、资讯搜索:谷歌一位员工说,在互联网上&#xff0…

Spring Cloud Alibaba-全面详解(学习总结---从入门到深化)

​​​​​​​ Spring Cloud Alibaba简介 什么是Spring Cloud Alibaba Spring Cloud Alibaba致力于提供微服务开发的一站式解决方案。 此项目包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 为…

OSI七层模型中各层网络协议

应用层: (典型设备:应用程序,如FTP,SMTP ,HTTP) DHCP(Dynamic Host Configuration Protocol)动态主机分配协议,使用 UDP 协议工作,主要有两个用途:给内部网络或网络服务供应商自动分配 IP 地址&#xff0c…

spring——Spring 注入内部Bean——setter 方式注入内部 Bean

我们将定义在 <bean> 元素的 <property> 或 <constructor-arg> 元素内部的 Bean&#xff0c;称为“内部 Bean”。 项目依赖&#xff1a; <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org…

交战“低代码”,云大厂跑马圈地

者 关注 2022年末,云大厂阿里、腾讯、华为(ATH)开始了新一轮的跑马圈地。 这一次交战的战场,是低代码。 11月18日,华为AppCube全线产品全面升级,在低代码、零代码、数据看板三个方面,升级优化;11月13日,腾讯升级了云开发开发者工具“微搭”,目前已服务开发者数300万…

疫情期间,家中常备药物有哪些?这份清单请收好

【防疫科普】疫情期间&#xff0c;家中常备药物有哪些&#xff1f;这份清单请收好 为加强防疫知识科普&#xff0c;提振群众战“疫”必胜信心&#xff0c;疫情期间&#xff0c;我们将持续向广大市民朋友传递科学健康科普知识、防疫提醒、自我防护注意事项、疫情期间心理疏导等相…

Python融于ASP框架

一、ASP的平反 想到ASP 很多人会说 “asp语言很蛋疼&#xff0c;不能面向对象&#xff0c;功能单一&#xff0c;很多东西实现不了” 等等诸如此类。 以上说法都是错误的&#xff0c;其一ASp不是一种语言是 微软用来代替CGI的一种web框架&#xff0c;只不过我们一直被扭曲在 vbs…

计算机毕业设计php_thinkphp_vue的校园论坛网站

运行环境 开发语言&#xff1a;PHP 数据库:MYSQL数据库 应用服务:apache服务器 使用框架:ThinkPHP&#xff1a;vue 开发工具:VScode/Dreamweaver/PhpStorm等均可 项目简介 在各学校的校园论坛中,交流是一项非常重要的事情。随着计算机多媒体技术的发展和网络的普及。采用当前流…

Python 数据库开发实战 - Redis命令行客户端与图形客户端的简单使用

上一章节我们已经启动了 redis 服务器&#xff0c;在这一章节我们就爱你过来学习 redis命令行客户端与图形客户端的简单使用&#xff0c;以及 redis 的 一些关键参数。 Redis 命令行客户端 - redis-cli redis-cli 是 Redis 自带的 命令行终端界面&#xff0c;一个简单的程序&…

【Redis】Redis 分布式锁误删问题

本文主要介绍 Redis 分布式锁误删问题的解决 场景一 1. 问题的产生情况一 因为业务阻塞&#xff0c;导致别人的锁被误删 2. 解决思路 获取锁的时候存入标识&#xff0c;释放锁的时候判断标识是否一致&#xff0c;一致可以释放锁&#xff0c;不一致不释放锁。 3. 解决代码 …

今年,我只赚了一点点

大家好&#xff0c;我是 Jack。 之前一直有小伙伴问我&#xff0c;有没有免费的股票信息查询的 API 接口&#xff1f; 我看了一圈&#xff0c;很多免费的 API 接口都年久失修&#xff0c;失效了。 那好吧&#xff0c;咱自己写一个。 想要玩量化交易&#xff0c;第一步&…

015 | 乡村振兴背景下田园综合体建设对农户生计的影响 | 大学生创新训练项目申请书 | 极致技术工厂

研究目的 本项目旨在研究xx市xx区田园综合体建设对农户生计的影响。以农户生计资本和生计恢复力为切入点&#xff0c;以塘坝镇、崇龛镇、太安镇等六个地级镇田园综合体建设前后作对比&#xff0c;研究其田园综合体建设对农户生计的影响&#xff0c;以期找到乡村振兴背景下田园综…

前端岗位编写代码注意规范

&#x1f525;&#x1f525;&#x1f525;关注前端小王hs&#x1f525;&#x1f525;&#x1f525; &#x1f525;&#x1f525;&#x1f525;加底部微信&#x1f525;&#x1f525;&#x1f525; &#x1f525;&#x1f525;&#x1f525;学习指导/前端练手项目推荐/毕设选题/…

基于元胞自动机森林火灾模型及应用(Matlab代码实现)

&#x1f468;‍&#x1f393;个人主页&#xff1a;研学社的博客 &#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜…

Allegro如何打印光绘层操作指导export模式

Allegro如何打印光绘层操作指导export模式 Allegro支持把光绘层面输出成pdf格式,下面介绍export模式如何输出,具体操作如下 打开光绘绘设置 确保光绘设置都是正确的 选择File-Export-PDF 弹出allegro PDF publisher,选择page setup Paper size选择A4,A4纸张 orientati…

SimKD

又搬来一个简单高效的知识蒸馏技术哦~~直接复用教师分类器还能显著减小性能差距的~ 在分类器的上一层通过特征对齐来训练学生模型&#xff0c;并直接复用教师分类器到学生模型中&#xff0c;再使用L2损失进行特征对齐。来自浙江大学的复用教师模型的方法哦~~ 浙大好厉害~~ 论…

2023年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

文章有点长&#xff0c;请耐心看完&#xff0c;绝对有收获&#xff01;不想听我BB直接进入面试分享&#xff1a; 准备过程 蚂蚁金服面试分享 拼多多面试分享 字节跳动面试分享 总结 说起来开始进行面试是年前倒数第二周&#xff0c;上午9点&#xff0c;我还在去公司的公交…

ERD Online 4.0.5 在线数据库建模、元数据管理(免费、私有部署)

4.0.5版本来袭❝ fix(erd): 增加数据库数据查询功能&#xff0c;支持多数据源切换查询&#xff0c;查看sql执行计划fix(erd): 数据查询功能&#xff0c;保留历史查询记录&#xff0c;格式化sql&#xff0c;多级树结构保存历史查询fix(erd): 依赖ERD加密手段&#xff0c;导出保留…

vdbench测试SSD快速入门

介绍 vdbench是一个I/O工作负载生成器&#xff0c;通常用于验证数据完整性和度量直接附加&#xff08;或网络连接&#xff09;存储性能。它可以运行在windows、linux环境&#xff0c;可用于测试文件系统或块设备基准性能。我们下面主要以块设备为介绍对象。 下载及安装 下载…

Linux 在过去几年发生的六种变化

随着时间的推移&#xff0c;Linux 桌面已经发生了变化&#xff0c;这种变化是逐渐发生的&#xff0c;因此这里汇总了过去十年中 Linux 桌面体验发生变化的一些具体方式。资深用户知道 Linux 桌面已经走过了漫长的道路。从前端应用程序设计到后端 Linux 组件&#xff0c;近年来发…