File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments