asyncio unicode

 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
34
import asyncio
from aiohttp import web
@asyncio.coroutine
def hello(request):
return web.Response(body=b"Hello, world")
@asyncio.coroutine
def variable_handler(request):
name = request.match_info.get('name', "Anonymous")
text = "Hello, " + name
return web.Response(body=text.encode('utf-8'))
@asyncio.coroutine
def variable_handler_uni(request):
"CONTENT-TYPE:text/plain; charset=utf-8"
return web.Response(
text="Hello, {}".format(request.match_info['name']))
app = web.Application()
app.router.add_route('GET', '/{name}', variable_handler)
app.router.add_route('GET', '/uni/{name}', variable_handler_uni)
loop = asyncio.get_event_loop()
f = loop.create_server(app.make_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