Created
November 5, 2018 07:03
-
-
Save oxlade39/4a699996a3d987d5e1ffc087fc76d788 to your computer and use it in GitHub Desktop.
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 aiohttp | |
import asyncio | |
async def fetch(session, url): | |
async with session.get(url) as response: | |
return await response.text() | |
async def main(): | |
async with aiohttp.ClientSession() as session: | |
results = [wait_for_code(404, session, 'http://python.org') for i in range(2)] | |
[print(result) for result in await asyncio.gather(*results)] | |
async def wait_for_code(code, session, url, step=1, retries=10): | |
async with session.get(url) as response: | |
count = retries | |
while(count > 0): | |
if response.status != code: | |
count = count - 1 | |
print("no match after {0} attempts. {1} attempts left".format(retries - count, count)) | |
await asyncio.sleep(step) | |
else: | |
return await response.text() | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment