使用 aiohttp 转储请求标头 [英] Dumping the request headers with aiohttp
问题描述
我想显示请求的所有 HTTP 标头(我添加的和自动生成的标头).我尝试使用跟踪 (https://aiohttp.readthedocs.io/en/stable/tracing_reference.html#aiohttp-client-tracing-reference) :
I want to display all the HTTP headers (the ones I added and the auto generated) of a request. I tried using traces (https://aiohttp.readthedocs.io/en/stable/tracing_reference.html#aiohttp-client-tracing-reference) :
#!/usr/bin/env python3
import aiohttp
import asyncio
async def on_request_start(session, trace_config_ctx, params):
print("Starting %s request for %s. I will send: %s" % (params.method, params.url, params.headers))
async def on_request_end(session, trace_config_ctx, params):
print("Ending %s request for %s. I sent: %s" % (params.method, params.url, params.headers))
async def fetch(session, url):
async with session.get(url) as response:
return response
async def main():
trace_config = aiohttp.TraceConfig()
trace_config.on_request_start.append(on_request_start)
trace_config.on_request_end.append(on_request_end)
async with aiohttp.ClientSession(trace_configs=[trace_config]) as session:
r = await fetch(session, 'http://stackoverflow.com')
print(r)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
使用此代码,我获得了方法和 URL,但标题的 dict 始终为空:
With this code, I get the method and the URL but the dict of headers is always empty:
% ./test-debug.py
Starting GET request for http://stackoverflow.com. I will send: <CIMultiDict()>
Ending GET request for https://stackoverflow.com/. I sent: <CIMultiDict()>
我错过了什么?
Python 3.7.2
Python 3.7.2
% pip show aiohttp
Name: aiohttp
Version: 3.5.4
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91@gmail.com
License: Apache 2
Location: /usr/lib/python3.7/site-packages
Requires: async-timeout, attrs, multidict, yarl, chardet
Required-by:
推荐答案
仔细阅读库源代码后,request_start
为时过早,它甚至在请求对象创建之前就被调用了,所以它永远不会看到完整的请求及其标头;定时器启动后,循环发送东西.
After having read library source code carefully, request_start
is too early, it is called even before the request object is created, so it will never see the full request and its headers; the timer is started after and the loop to send stuff.
但是在 request_end
中,您可以访问完整的响应对象,该对象与请求对象以及所有标头相关联.
But in request_end
you have access to the full response object, which is tied to the request object and hence all headers.
有了这个变化:
async def on_request_end(session, trace_config_ctx, params):
print("Ending %s request for %s. I sent: %s" % (params.method, params.url, params.headers))
print('Sent headers: %s' % params.response.request_info.headers)
我明白了:
Sent headers: <CIMultiDictProxy('Host': 'stackoverflow.com', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Python/3.7 aiohttp/3.5.4', 'Cookie': 'prov=f4fad342-c1f7-bcc2-5d25-0e30ae5cdbf6')>
如果发生重定向,您可能还需要查看 params.response.history
.它是一系列 ClientResponse
对象,因此您应该能够对每个对象调用 request_info.headers
.
You may also need to look at params.response.history
in case of redirects. It is a sequence of ClientResponse
object so you should be able to call request_info.headers
on each of them.
这篇关于使用 aiohttp 转储请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!