如果服务器端代码如下,则浏览器访问的时候,总是下载文件。
需要加上
content_type='text/html'
比如:
return web.Response(body=b'<h1>Index</h1>', content_type='text/html')
就能解决问题。
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 async def index(request): 15 await asyncio.sleep(0.5) 16 return web.Response(body=b'<h1>Index</h1>') 17 18 async def hello(request): 19 await asyncio.sleep(0.5) 20 text = '<h1>hello, %s!</h1>' % request.match_info['name'] 21 return web.Response(body=text.encode('utf-8')) 22 23 async 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 = await 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()