Skip to content

Instantly share code, notes, and snippets.

View dgtlctzn's full-sized avatar

Joseph Perry dgtlctzn

View GitHub Profile
@dgtlctzn
dgtlctzn / install.sh
Created December 3, 2021 17:07
Install
$ pip install aiohttp
@dgtlctzn
dgtlctzn / get_url.py
Created December 3, 2021 17:06
Get Url
async def get_url(session: aiohttp.ClientSession, url: str) -> Dict:
async with session.get(url) as response:
return await response.json()
@dgtlctzn
dgtlctzn / status_code.py
Last active December 3, 2021 17:06
Status Codes
...
async with session.get(url) as response:
if response.status == 503:
# do some work
@dgtlctzn
dgtlctzn / post.py
Created December 3, 2021 17:04
Post Request
async with session.post(url, data=payload) as response:
...
@dgtlctzn
dgtlctzn / tasks.py
Created December 3, 2021 17:03
Tasks
async def request_urls(urls: List[str]):
async with aiohttp.ClientSession() as session:
tasks: List[asyncio.Task] = []
for url in urls:
tasks.append(
asyncio.ensure_future(
get_url(session, url)
)
)
return await asyncio.gather(*tasks)
@dgtlctzn
dgtlctzn / headers_cookies.py
Created December 3, 2021 17:02
Headers and Cookies
async with aiohttp.ClientSession(
headers=headers_dict,
cookies=cookies_dict,
trace_configs=[trace_config],
) as session:
...
@dgtlctzn
dgtlctzn / future.py
Created December 3, 2021 17:02
Future
task: asyncio.Task = asyncio.ensure_future(
get_url(session, url)
)
@dgtlctzn
dgtlctzn / gather.py
Created December 3, 2021 16:59
Gather
async def request_urls(urls: List[str]):
tasks: List[asyncio.Task] = []
...
return await asyncio.gather(*tasks)
@dgtlctzn
dgtlctzn / responses.py
Created December 3, 2021 16:58
Responses
responses: List[Dict] = asyncio.run(request_urls(urls))
@dgtlctzn
dgtlctzn / time_test.py
Created December 3, 2021 16:57
Time Test
from time import time
responses: List[Dict] = []
start_time: float = time()
for url in URLS:
responses.append(requests.get(url).json())
end_time: float = time()