Skip to content

Instantly share code, notes, and snippets.

@jacobbridges
Created March 23, 2016 00:57
Show Gist options
  • Save jacobbridges/c1b1c38edf99c0428b19 to your computer and use it in GitHub Desktop.
Save jacobbridges/c1b1c38edf99c0428b19 to your computer and use it in GitHub Desktop.
import json
import asyncio
from aiohttp import web, get
async def hello(request: web.Request):
print(request.headers)
return web.Response(
body=bytes(json.dumps(dict(
name='veggiecron-server',
author='Jacob Bridges',
version='1.0.0a',
last_updated='Jan 10, 2016',
)), 'utf-8'),
content_type='application/json'
)
async def pull(request: web.Request):
print(request.GET)
responses = []
for i in range(int(request.GET['clones'])):
responses.append(await (await get(request.GET['url'])).text())
return web.Response(
body=bytes(json.dumps(dict(
responses=[r for r in responses],
)), 'utf-8'),
content_type='application/json'
)
app = web.Application()
app.router.add_route('GET', '/', hello)
app.router.add_route('GET', '/pull', pull)
loop = asyncio.get_event_loop()
handler = app.make_handler()
f = loop.create_server(handler, '0.0.0.0', 8080)
srv = loop.run_until_complete(f)
print('serving on', srv.sockets[0].getsockname())
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
srv.close()
loop.run_until_complete(srv.wait_closed())
loop.run_until_complete(handler.finish_connections(1.0))
loop.run_until_complete(app.finish())
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment