1 | """ aiohttp examples """
|
---|
2 |
|
---|
3 | import sys
|
---|
4 |
|
---|
5 | import asyncio
|
---|
6 | import aiohttp
|
---|
7 | from aiohttp import web
|
---|
8 |
|
---|
9 |
|
---|
10 | BIN = "http://requestb.in/1dbep2b1"
|
---|
11 |
|
---|
12 |
|
---|
13 | async def simple_get():
|
---|
14 | """ Simple .get request. """
|
---|
15 | response = await aiohttp.get(BIN)
|
---|
16 | text = await response.text()
|
---|
17 | return text
|
---|
18 |
|
---|
19 |
|
---|
20 | async def simple_post():
|
---|
21 | """ Simple .post. """
|
---|
22 | response = await aiohttp.post(BIN, data=b'DATA!')
|
---|
23 | text = await response.text()
|
---|
24 | return response
|
---|
25 |
|
---|
26 |
|
---|
27 | async def url_params():
|
---|
28 | """ URL params. """
|
---|
29 | params = {'hello': 'ctpug', 'year': '2016'}
|
---|
30 | response = await aiohttp.get(BIN, params=params)
|
---|
31 | text = await response.text()
|
---|
32 | return response
|
---|
33 |
|
---|
34 |
|
---|
35 | async def release():
|
---|
36 | """ Explicitly releasing the response """
|
---|
37 | async with aiohttp.get(BIN) as response:
|
---|
38 | return response
|
---|
39 |
|
---|
40 |
|
---|
41 | def loop(d):
|
---|
42 | loop = asyncio.get_event_loop()
|
---|
43 | return loop.run_until_complete(d)
|
---|
44 |
|
---|
45 |
|
---|
46 | def run_example():
|
---|
47 | [name] = sys.argv[1:]
|
---|
48 | example = globals()[name]
|
---|
49 | print(loop(example()))
|
---|
50 |
|
---|
51 |
|
---|
52 | if __name__ == '__main__':
|
---|
53 | run_example()
|
---|