Skip to content

Commit 64c91fb

Browse files
committed
add aio http sample
1 parent a52ba6f commit 64c91fb

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

samples/web/aio_web.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = 'Michael Liao'
5+
6+
'''
7+
async web application.
8+
'''
9+
10+
import asyncio
11+
12+
from aiohttp import web
13+
14+
def index(request):
15+
return web.Response(body=b'<h1>Index</h1>')
16+
17+
def hello(request):
18+
yield from asyncio.sleep(0.5)
19+
text = '<h1>hello, %s!</h1>' % request.match_info['name']
20+
return web.Response(body=text.encode('utf-8'))
21+
22+
@asyncio.coroutine
23+
def init(loop):
24+
app = web.Application(loop=loop)
25+
app.router.add_route('GET', '/', index)
26+
app.router.add_route('GET', '/hello/{name}', hello)
27+
srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 8000)
28+
print('Server started at http://127.0.0.1:8000...')
29+
return srv
30+
31+
loop = asyncio.get_event_loop()
32+
loop.run_until_complete(init(loop))
33+
loop.run_forever()

0 commit comments

Comments
 (0)