Skip to content

Commit 5f19535

Browse files
committed
aiohttp示例代码
1 parent a54ad4c commit 5f19535

File tree

2 files changed

+145
-30
lines changed

2 files changed

+145
-30
lines changed

.idea/workspace.xml

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

0 commit comments

Comments
 (0)