zoukankan      html  css  js  c++  java
  • 访问服务器,但是总下载文件,而不是在浏览器中显示

    如果服务器端代码如下,则浏览器访问的时候,总是下载文件。
    需要加上
    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()
  • 相关阅读:
    poj 3159 Candies
    强连通分量——Tarjan算法
    nyoj 次方求模
    nyoj 快速查找素数
    nyoj 光棍节的快乐
    拓扑排序
    快速幂取模
    nyoj 最大素因子
    素数打表
    nyoj 数的长度
  • 原文地址:https://www.cnblogs.com/andy-0212/p/9940756.html
Copyright © 2011-2022 走看看