Created
May 24, 2016 12:31
-
-
Save ericfourrier/2acaeb1164244b1085755d5634e3035f 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 asyncio | |
import aiohttp | |
from aiohttp import web | |
import json | |
WEBSITES = ['http://example.com/', 'http://dummy-a98x3.org', 'http://example.net/'] | |
async def handle(request): | |
# Fire up 3 requests in parallel | |
coroutines = [aiohttp.request('get', website) for website in WEBSITES] | |
# Wait for those requests to complete | |
results = await asyncio.gather(*coroutines, return_exceptions=True) | |
# Analyze results | |
response_data = { | |
website: not isinstance(result, Exception) and result.status == 200 | |
for website, result in zip(WEBSITES, results) | |
} | |
# Build JSON response | |
body = json.dumps(response_data).encode('utf-8') | |
return web.Response(body=body, content_type="application/json") | |
loop = asyncio.get_event_loop() | |
app = web.Application(loop=loop) | |
app.router.add_route('GET', '/', handle) | |
server = loop.create_server(app.make_handler(), '127.0.0.1', 8000) | |
print("Server started at http://127.0.0.1:8000") | |
loop.run_until_complete(server) | |
try: | |
loop.run_forever() | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment