【问题标题】:How to feed client request headers and server response headers in aiohttp server response body?如何在 aiohttp 服务器响应正文中提供客户端请求标头和服务器响应标头?
【发布时间】:2017-04-13 18:19:40
【问题描述】:

下面是一个简单的aiohttp服务器代码,我想知道如何在服务器的响应中返回所有客户端的http请求头信息和服务器响应http头信息。

一个简单的目标是当我使用网络浏览器打开http://127.0.0.1:8080时,网页可以立即显示客户端的http请求头和服务器响应的http头。

感谢任何帮助。

from aiohttp import web

async def handle(request):

    request_head = web.Request.headers          //Q1?
    response_head = web.Response.headers        //Q2?

    return web.Response("\n".join((request_head,response_head)))

app = web.Application()
app.router.add_get('/', handle)

web.run_app(app)

【问题讨论】:

    标签: python http head aiohttp


    【解决方案1】:

    管理得到了一个肮脏的解决方案,在响应头部分存在缺陷 - 不太确定第二个 web.Response 是否会更改标头本身,感谢任何改进,谢谢。

    from aiohttp import web
    
    
    async def handle(request):
    
        request_head = tuple((k.encode('utf-8'), v.encode('utf-8')) for k, v in request.headers.items())
    
        resp = web.Response(text = str(request_head))
        response_head = tuple((k.encode('utf-8'), v.encode('utf-8')) for k, v in resp.headers.items())
    
        resp = web.Response(text = "\n".join((str(request_head),str(response_head))))
    
        return resp
    
    app = web.Application()
    app.router.add_get('/', handle)
    web.run_app(app)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 2010-11-13
      • 2016-06-04
      • 2017-07-11
      • 2021-10-18
      • 2018-04-24
      相关资源
      最近更新 更多