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