Last active
January 22, 2018 13:33
-
-
Save sbdzdz/67c2ff12d760c79deafe8d76849a56b2 to your computer and use it in GitHub Desktop.
Asynchronous and synchronous requests in Python 3.6
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 json | |
| import sys | |
| from asyncio import ensure_future | |
| from aiohttp import ClientSession | |
| async def fetch(domain, session): | |
| url = 'http://localhost:8080/allicons.json' | |
| params = {'url': domain, 'format': 'png'} | |
| async with session.get(url, params=params, timeout=600) as response: | |
| response = await response.json() | |
| icons = set(str(icon['height']) for icon in response.get('icons', [])) | |
| if icons: | |
| print(domain, ' '.join(icons)) | |
| async def run(domains): | |
| async with ClientSession() as session: | |
| tasks = [ensure_future(fetch(domain, session)) for domain in domains] | |
| try: | |
| await asyncio.gather(*tasks) | |
| except Exception as e: | |
| #print(e) | |
| pass | |
| def domains_from_stdin(stdin): | |
| return [line.split()[0] for line in stdin] | |
| if __name__ == '__main__': | |
| domains = domains_from_stdin(sys.stdin) | |
| loop = asyncio.get_event_loop() | |
| future = asyncio.ensure_future(run(domains)) | |
| loop.run_until_complete(future) |
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 json | |
| import requests | |
| import sys | |
| def get_icons(domain): | |
| url = 'http://localhost:8080/allicons.json?url={}&formats=png'.format(domain) | |
| req = requests.get(url) | |
| try: | |
| req_json = json.loads(req.text) | |
| except json.JSONDecodeError: | |
| return [] | |
| icons = set() | |
| for icon in req_json.get('icons', []): | |
| icons.add(icon['height']) | |
| return list(icons) | |
| if __name__ == '__main__': | |
| domain = sys.argv[1].strip().split()[0][1:] | |
| print(get_icons(domain)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment