Debian Codegraph @

third_party/python/aiohttp/examples/curl.py

raw
1 #!/usr/bin/env python3
2
3 import argparse
4 import asyncio
5 import sys
6
7 import aiohttp
8
9
10 async def curl(url):
11 async with aiohttp.ClientSession() as session:
12 async with session.request("GET", url) as response:
13 print(repr(response))
14 chunk = await response.content.read()
15 print("Downloaded: %s" % len(chunk))
16
17
18 if __name__ == "__main__":
19 ARGS = argparse.ArgumentParser(description="GET url example")
20 ARGS.add_argument("url", nargs=1, metavar="URL", help="URL to download")
21 ARGS.add_argument(
22 "--iocp",
23 default=False,
24 action="store_true",
25 help="Use ProactorEventLoop on Windows",
26 )
27 options = ARGS.parse_args()
28
29 if options.iocp and sys.platform == "win32":
30 from asyncio import events, windows_events
31
32 # https://github.com/python/mypy/issues/12286
33 el = windows_events.ProactorEventLoop() # type: ignore[attr-defined]
34 events.set_event_loop(el)
35
36 loop = asyncio.get_event_loop()
37 loop.run_until_complete(curl(options.url[0]))