访问服务器,但是总下载文件,而不是在浏览器中显示

如果服务器端代码如下,则浏览器访问的时候,总是下载文件。
需要加上
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()

 

转载于:https://www.cnblogs.com/andy-0212/p/9940756.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值