Skip to content

Commit bccf920

Browse files
committed
add python_aiohttp.py
1 parent c2de969 commit bccf920

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
### python_metaclass.py: Python进阶: 一步步理解Python中的元类metaclass
2828

2929
### python_coroutine.py: Python进阶:理解Python中的异步IO和协程(Coroutine), 并应用在爬虫中
30+
31+
### python_aiohttp.py: Python中最好用的异步爬虫库Aiohttp代码实例
3032
===================================================================================================
3133

3234
### 您可以fork该项目,并在修改后提交Pull request

python_aiohttp.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# _*_ coding: utf-8 _*_
2+
3+
"""
4+
python_aiohttp.py by xianhu
5+
"""
6+
7+
import asyncio
8+
import aiohttp
9+
10+
11+
# 简单实例
12+
async def aiohttp_test01(url):
13+
async with aiohttp.ClientSession() as session:
14+
async with session.get(url) as resp:
15+
print(resp.status)
16+
print(await resp.text())
17+
18+
loop = asyncio.get_event_loop()
19+
tasks = [aiohttp_test01("https://api.github.com/events")]
20+
loop.run_until_complete(asyncio.wait(tasks))
21+
loop.close()
22+
23+
# 其他Http方法
24+
# session.post('http://httpbin.org/post', data=b'data')
25+
# session.put('http://httpbin.org/put', data=b'data')
26+
# session.delete('http://httpbin.org/delete')
27+
# session.head('http://httpbin.org/get')
28+
# session.options('http://httpbin.org/get')
29+
# session.patch('http://httpbin.org/patch', data=b'data')
30+
31+
# 自定义Headers
32+
# payload = {'some': 'data'}
33+
# headers = {'content-type': 'application/json'}
34+
# await session.post(url, data=json.dumps(payload), headers=headers)
35+
36+
# 自定义Cookie
37+
# cookies = {'cookies_are': 'working'}
38+
# async with ClientSession(cookies=cookies) as session:
39+
# 访问Cookie: session.cookie_jar
40+
41+
# 在URLs中传递参数
42+
# 1. params = {'key1': 'value1', 'key2': 'value2'}
43+
# 2. params = [('key', 'value1'), ('key', 'value2')]
44+
# async with session.get('http://httpbin.org/get', params=params) as resp:
45+
# assert resp.url == 'http://httpbin.org/get?key2=value2&key1=value1'
46+
47+
# 发送数据
48+
# payload = {'key1': 'value1', 'key2': 'value2'}
49+
# async with session.post('http://httpbin.org/post', data=payload) as resp:
50+
# async with session.post(url, data=json.dumps(payload)) as resp:
51+
# print(await resp.text())
52+
53+
# 发送文件(1)
54+
# files = {'file': open('report.xls', 'rb')}
55+
# await session.post(url, data=files)
56+
57+
# 发送数据(2)
58+
# data = FormData()
59+
# data.add_field('file',
60+
# open('report.xls', 'rb'),
61+
# filename='report.xls',
62+
# content_type='application/vnd.ms-excel')
63+
# await session.post(url, data=data)
64+
65+
# 超时设置
66+
# aync with session.get('https://github.com', timeout=60) as r:
67+
68+
# 代理支持
69+
# async with aiohttp.ClientSession() as session:
70+
# async with session.get("http://python.org", proxy="http://some.proxy.com") as resp:
71+
# print(resp.status)
72+
73+
# async with aiohttp.ClientSession() as session:
74+
# proxy_auth = aiohttp.BasicAuth('user', 'pass')
75+
# async with session.get("http://python.org", proxy="http://some.proxy.com", proxy_auth=proxy_auth) as resp:
76+
# print(resp.status)
77+
# session.get("http://python.org", proxy="http://user:[email protected]")
78+
79+
# 返回的内容
80+
# async with session.get('https://api.github.com/events') as resp:
81+
# print(await resp.text())
82+
# print(await resp.text(encoding='gbk'))
83+
# print(await resp.read())
84+
# print(await resp.json())
85+
86+
# 返回内容较大
87+
# with open(filename, 'wb') as fd:
88+
# while True:
89+
# chunk = await resp.content.read(chunk_size)
90+
# if not chunk:
91+
# break
92+
# fd.write(chunk)
93+
94+
# 返回的其他变量
95+
# async with session.get('http://httpbin.org/get') as resp:
96+
# print(resp.status) # 状态码
97+
# print(resp.headers) # Headers
98+
# print(resp.raw_headers) # 原始Headers
99+
# print(resp.cookies) # 返回的Cookie
100+
101+
# 访问历史History
102+
# resp = await session.get('http://example.com/some/redirect/')
103+
# resp: <ClientResponse(http://example.com/some/other/url/) [200]>
104+
# resp.history: (<ClientResponse(http://example.com/some/redirect/) [301]>,)
105+
106+
# 释放返回的Response
107+
# 1. async with session.get(url) as resp: pass
108+
# 2. await resp.release()
109+
110+
# 连接器: Connectors
111+
# conn = aiohttp.TCPConnector()
112+
# session = aiohttp.ClientSession(connector=conn)
113+
114+
# 限制连接池大小:
115+
# conn = aiohttp.TCPConnector(limit=30)
116+
# conn = aiohttp.TCPConnector(limit=None)

0 commit comments

Comments
 (0)