[英]How can I see the actual data Requests sends over the wire?
(This is a follow-up question to this one ) (这是一个后续问题这一个)
How can I tell urllib3
to log the FULL request, including, but not limited to:我如何告诉
urllib3
记录FULL请求,包括但不限于:
I am having trouble connecting to LinkedIn with OAuth (a similar implementation works with Google and Facebook), and I would like to see exactly what requests are being sent.我无法使用 OAuth 连接到 LinkedIn(类似的实现适用于 Google 和 Facebook),我想确切地查看正在发送的请求。 I suspect the
auth_token
is not being provided, but I need to confirm this.我怀疑未提供
auth_token
,但我需要确认这一点。 For that, I need urllib3
to show the full requests, since they are over HTTPS and I can not analyze network traffic to see them (end-to-end encryption).为此,我需要
urllib3
来显示完整的请求,因为它们是通过 HTTPS 的,我无法分析网络流量来查看它们(端到端加密)。
您可以使用request
访问器访问在请求发生后实际发送的PreparedRequest
对象,例如print dir(r.request)
。
You can hack into the requests
hooks system instead, then use that to track responses and their requests:您可以改用
requests
钩子系统,然后使用它来跟踪响应及其请求:
from requests import hooks
_orig_default_hooks = hooks.default_hooks
def my_default_hooks():
hooks = _orig_default_hooks()
hooks['response'].append(response_hook)
return hooks
hooks.default_hooks = my_default_hooks
# requests.models is imported by the requests package, so we need to ensure it's reference
# to default_hooks is updated too.
import requests.models
requests.models.default_hooks = my_default_hooks
def response_hook(r, **kw):
req = r.request
print req.headers
The reqeuests.hooks.default_hooks()
function is called for each and every request created, and by injecting your own response
event hook you get to see each and every response received. reqeuests.hooks.default_hooks()
函数为每个创建的请求调用,通过注入你自己的response
事件钩子,你可以看到收到的每个响应。 Responses have a .request
attribute, which is a PreparedRequest
instance, on which you'll find the .headers
and .body
attributes for your inspection.响应有一个
.request
属性,它是一个PreparedRequest
实例,您可以在其中找到.headers
和.body
属性以供检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.