-
Notifications
You must be signed in to change notification settings - Fork 3
/
aio_web.py
33 lines (25 loc) · 842 Bytes
/
aio_web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'Carlo'
'''
async web application
'''
import asyncio
from aiohttp import web
async def index(request):
await asyncio.sleep(0.5)
return web.Response(headers={'content-type':'text/html'},body='<h1>Index</h1>')
async def hello(request):
await asyncio.sleep(0.5)
text = '<h1>hello, %s! </h1>' % request.match_info['name']
return web.Response(body=text.encode('utf-8'))
async def init(loop):
app = web.Application(loop=loop)
app.router.add_route('GET', '/', index)
app.router.add_route('GET','/hello/{name}', hello)
srv = await loop.create_server(app.make_handler(), '127.0.0.1', 5000)
print('Server started at http://127.0.0.1:5000...')
return srv
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()