aiohttp使用教程

aiohttp使用教程

aiohttp分为服务器端和客户端,本文只介绍客户端。
前面有一章文章介绍了协程

1.1、请求用法

import asyncio
import requests
import aiohttp
async def request():
	async with aiohttp.ClientSession() as session:
		url = 'http://httpbin.org'
		async with session.get(url) as response:
			print(response.status)
			print(response.text())
tasks = [asyncio.ensure_future(request()) for _ in range(5)]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
  • 其中response.text()可以在括号内指定解码方式,编码方式。
  • 其中response.read()适合读取图像,是无法编码
  1. 第五行:创建一个CelientSession对象命名为session
  2. 第七行:通过sessionget方法得到一个ClientResponse对象,命名为resp

除了get请求还有post请求

**注意:**不要为每次链接都创建一次session,一般情况下要创建一个session,然后使用这个session执行所有请求。
每个session对象,内部创建一个连接池,并且会将保持连接和连接复用默认开始可以加快整体性能。

1.2 在URL中传递参数

	async with aiohttp.ClientSession() as session:
		params = {'a':1,'b':2,'c':3}
		url = 'http://httpbin.org/get'
		async with session.get(url,params=params) as response:
			print(response.url)

1.3、相应内容

	async with aiohttp.ClientSession() as session:
		url = 'http://httpbin.org/get'
		async with session.get(url) as response:
			print(await response.text())

await response.text(encoding='gb2312')方法,会将服务器端返回的内容进行解码,可以指定编码方式。
await response.read()方法,获取的类型是字节的内容。

其中text() read()是将相体毒读入内存,如果是获取大量数据,使用字节流streaming response

1.3、特殊响应内容:json

	async with aiohttp.ClientSession() as session:
		url = 'https://api.github.com/events'
		async with session.get(url) as response:
			print(await response.json())

内置的方法处理json

1.4、字节流读取响应内容

	async with aiohttp.ClientSession() as session:
		url = 'http://httpbin.org/get'
		async with session.get(url) as response:
			with open('demo.txt','wb') as f:
				while True:
					chunk = await response.content.read(1024)
					if not chunk:
						break
					f.write(chunk)

如果是向下载打大内容的文件,上面的方式是直接向响应体读入到内存,用字节流控制字节数来控制读入内存的响应内容。

1.5、自定义头

	async with aiohttp.ClientSession() as session:
		url = 'http://httpbin.org/post'
		data = {'a':1,'b':2}
		headers = {'content-type': 'application/json'}
		async with session.post(url,data=json.dumps(data),headers=headers) as response:
			print(await response.text())

1.6、自定义Cookie

async def request():
	url = 'http://httpbin.org/cookies'
	cookies = {'cookie_demo':'demo'}
	async with aiohttp.ClientSession(cookies=cookies) as session:
		async with session.get(url) as response:
			print(await response.json())

1.7、post数据的方式

1.模拟表单post数据

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

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

2.post JSON

	async with aiohttp.ClientSession() as session:
		url = 'http://httpbin.org/post'
		data = {'a':1,'b':2}
		headers = {'content-type': 'application/json'}
		async with session.post(url,data=json.dumps(data),headers=headers) as response:
			print(await response.text())

3.post 小文件

	async with aiohttp.ClientSession() as session:
		url = 'http://httpbin.org/post'
		data = aiohttp.FormData()
		data.add_field('file',
			open('demo.txt', 'rb'),
			filename = 'demo.txt',
			content_type='application/vnd.ms-excel',
			)
		async with session.post(url,data=data) as response:
			print(await response.text())
files = {'file': open('report.xls', 'rb')}
await session.post(url, data=files)

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

1.8、设置代理

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")

1.9返回内容查看

  • resp.status:查看状态码
  • resp.headers:查看headers
  • resp.cookie:查看cookie
  • 超时处理session.get('http://httpbin.org/get', timeout=60)
  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值