简体   繁体   English

如何查看 Requests 通过网络发送的实际数据?

[英]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请求,包括但不限于:

  • URL网址
  • query parameters查询参数
  • headers标题
  • body身体
  • and anything else which is sent inside the request (I am not sure there is anything else, but if there is something else, I also want to see it)以及在请求中发送的任何其他内容(我不确定是否还有其他内容,但如果有其他内容,我也想查看)

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.

相关问题 如果 y_test 数据是预测结果,我怎样才能看到实际结果? - if y_test data is the predicted results, how can I see the actual results? 如何在 Python 中看到 0.1 的实际位序列? - How I can see the actual bit sequence of 0.1 in Python? 如何在Python请求的请求标头中看到ip地址? - How can I see ip address in request header in Python requests? Cloudwatch 发送到 SNS 的 JSON 有效负载中有什么? 我怎样才能读取这些数据? - What is in the JSON payload Cloudwatch sends to SNS? How can I read that data? 我如何使用 ADF 从将响应作为 xml 发送到 SQL 服务器的端点复制数据? - How can i copy data from an endpoint which sends response as an xml to a SQL server using ADF? Python请求获取与我在浏览器中看到的数据不同的数据 - Python requests fetching data different than what I see on browser 如何查看Dask DataFrame的数据预览? - How can I see the data preview of Dask DataFrame? 如何查看在Chrome中提交的django表单的请求数据? - How can i see the request data of a django form submit in Chrome? 如何查看请求数据? - How can see the request data? 如何使用Python的要求抓取超市的营养数据? - How can I scrape supermarket nutrient data with Python's requests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM