Created
March 22, 2020 08:40
-
-
Save gnilchee/63e8c7d605ae221c1aecfc1c326d759d to your computer and use it in GitHub Desktop.
Use asyncio and aiohttp to grab status codes from 20 sites
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aiohttp | |
my_urls = [ | |
'https://www.google.com/', | |
'https://www.youtube.com/', | |
'https://www.facebook.com/', | |
'https://www.wikipedia.org/', | |
'https://www.yahoo.com/', | |
'https://www.reddit.com/', | |
'https://www.amazon.com/', | |
'https://github.com/', | |
'https://twitter.com', | |
'https://news.google.com/', | |
'https://www.instagram.com/', | |
'https://httpbin.org', | |
'https://www.linkedin.com/', | |
'https://www.netflix.com/', | |
'https://imgur.com/', | |
'https://www.ebay.com/', | |
'https://www.apple.com/', | |
'https://wordpress.com/', | |
'https://www.bing.com/', | |
'https://www.microsoft.com/' | |
] | |
# valid user-agent to ensure proper response | |
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'} | |
async def fetch(session, url): | |
async with session.get(url) as response: | |
return response | |
async def fetch_all(loop, session, urls): | |
tasks = [] | |
for url in urls: | |
task = loop.create_task(fetch(session, url)) | |
tasks.append(task) | |
results = await asyncio.gather(*tasks) | |
return results | |
async def main(loop): | |
async with aiohttp.ClientSession(headers=headers) as session: | |
res = await fetch_all(loop, session, my_urls) | |
for r in res: | |
print("{} - {}".format(r.url, r.status)) | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main(loop)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# time python3 get_urls.py | |
https://www.google.com/ - 200 | |
https://www.youtube.com/ - 200 | |
https://www.facebook.com/ - 200 | |
https://www.wikipedia.org/ - 200 | |
https://www.yahoo.com/ - 200 | |
https://www.reddit.com/ - 200 | |
https://www.amazon.com/ - 200 | |
https://github.com/ - 200 | |
https://twitter.com - 200 | |
https://news.google.com/?hl=en-US&gl=US&ceid=US:en - 200 | |
https://www.instagram.com/ - 200 | |
https://httpbin.org - 200 | |
https://www.linkedin.com/ - 200 | |
https://www.netflix.com/ - 200 | |
https://imgur.com/ - 200 | |
https://www.ebay.com/ - 200 | |
https://www.apple.com/ - 200 | |
https://wordpress.com/ - 200 | |
https://www.bing.com/ - 200 | |
https://www.microsoft.com/en-us/ - 200 | |
real 0m1.118s | |
user 0m0.337s | |
sys 0m0.068s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment