Skip to content

Instantly share code, notes, and snippets.

View dgtlctzn's full-sized avatar

Joseph Perry dgtlctzn

View GitHub Profile
@dgtlctzn
dgtlctzn / async_crypto.py
Last active December 3, 2021 16:53
All Together Now
import asyncio
import aiohttp
CRYPTOS: List[str] = [
"BTC",
"ETH",
"DOGE",
"BCH",
"ETC",
"LTC",
$ python3 async_crypto.py
[{'data': {'base': 'BTC', 'currency': 'USD', 'amount': '58995.78'}},
{'data': {'base': 'ETH', 'currency': 'USD', 'amount': '4467.16'}},
{'data': {'base': 'DOGE', 'currency': 'USD', 'amount': '0.22'}},
{'data': {'base': 'BCH', 'currency': 'USD', 'amount': '582.49'}},
{'data': {'base': 'ETC', 'currency': 'USD', 'amount': '48.59'}},
{'data': {'base': 'LTC', 'currency': 'USD', 'amount': '208.84'}}]
@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()
@dgtlctzn
dgtlctzn / responses.py
Created December 3, 2021 16:58
Responses
responses: List[Dict] = asyncio.run(request_urls(urls))
@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 / future.py
Created December 3, 2021 17:02
Future
task: asyncio.Task = asyncio.ensure_future(
get_url(session, url)
)
@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 / 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 / post.py
Created December 3, 2021 17:04
Post Request
async with session.post(url, data=payload) as response:
...
@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