如何查看Python应用程序发送的整个HTTP请求?

Chr*_* B. 236 python debugging https python-requests

就我而言,我正在使用该requests库通过HTTPS调用PayPal的API.不幸的是,我从PayPal收到错误,并且PayPal支持无法弄清楚错误是什么或导致错误.他们要我"请提供整个请求,包括标题".

我怎样才能做到这一点?

Ina*_*ist 459

一种简单的方法:启用最近版本的请求(1.x和更高版本)的日志记录.

请求使用http.clientlogging模块配置成控制日志记录级别,如所描述这里.

示范

摘自链接文档的代码:

import requests
import logging

# These two lines enable debugging at httplib level (requests->urllib3->http.client)
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# The only thing missing will be the response.body which is not logged.
try:
    import http.client as http_client
except ImportError:
    # Python 2
    import httplib as http_client
http_client.HTTPConnection.debuglevel = 1

# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

requests.get('https://httpbin.org/headers')
Run Code Online (Sandbox Code Playgroud)

示例输出

$ python requests-logging.py 
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): httpbin.org
send: 'GET /headers HTTP/1.1\r\nHost: httpbin.org\r\nAccept-Encoding: gzip, deflate, compress\r\nAccept: */*\r\nUser-Agent: python-requests/1.2.0 CPython/2.7.3 Linux/3.2.0-48-generic\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Content-Type: application/json
header: Date: Sat, 29 Jun 2013 11:19:34 GMT
header: Server: gunicorn/0.17.4
header: Content-Length: 226
header: Connection: keep-alive
DEBUG:requests.packages.urllib3.connectionpool:"GET /headers HTTP/1.1" 200 226
Run Code Online (Sandbox Code Playgroud)

  • 请注意,httplib在Python 3上不可用.要使代码可移植,请将`import httplib`替换为`import requests.packages.urllib3.connectionpool as httplib`或使用six和`from six.moves import http_client as httplib`. (9认同)
  • @shershen 发布的链接不再有效。这似乎是当前的替代品:https://requests.readthedocs.io/en/master/api/?highlight=debug#api-changes (4认同)
  • 对于Python3,请参阅此处 - http://docs.python-requests.org/en/latest/api/?highlight=debug ```from http.client import HTTPConnection``` (2认同)

Sky*_*and 128

r = requests.get('https://api.github.com', auth=('user', 'pass'))
Run Code Online (Sandbox Code Playgroud)

r是一个回应.它有一个请求属性,其中包含您需要的信息.

r.request.allow_redirects  r.request.headers          r.request.register_hook
r.request.auth             r.request.hooks            r.request.response
r.request.cert             r.request.method           r.request.send
r.request.config           r.request.params           r.request.sent
r.request.cookies          r.request.path_url         r.request.session
r.request.data             r.request.prefetch         r.request.timeout
r.request.deregister_hook  r.request.proxies          r.request.url
r.request.files            r.request.redirect         r.request.verify
Run Code Online (Sandbox Code Playgroud)

r.request.headers 给出标题:

{'Accept': '*/*',
 'Accept-Encoding': 'identity, deflate, compress, gzip',
 'Authorization': u'Basic dXNlcjpwYXNz',
 'User-Agent': 'python-requests/0.12.1'}
Run Code Online (Sandbox Code Playgroud)

然后r.request.data将身体作为映射.urllib.urlencode如果他们愿意,您可以转换它:

import urllib
b = r.request.data
encoded_body = urllib.urlencode(b)
Run Code Online (Sandbox Code Playgroud)

  • 在我的情况下,这是这样做的首选方式.只有一个注释:在我的情况下,`response.request`似乎是一个`PreparedRequest`; 它没有`.data`而是`.body`. (17认同)
  • 以下哪一项给了我"整个请求,包括标题"? (12认同)
  • 我不完全确定他们在寻找什么.我希望以一种确切的格式,逐字节地捕获所有通过线路传输的内容. (7认同)
  • 对于完整的URL(带有querystring参数),您还可以使用`response.url`(这有点不同,因为它不是`response.request ...`。 (2认同)

Gia*_*uca 14

调试 HTTP 本地请求的一种更简单的方法是使用 netcat。如果你跑

nc -l 1234
Run Code Online (Sandbox Code Playgroud)

您将开始侦听端口上1234的 HTTP 连接。您可以通过 访问它http://localhost:1234/foo/foo/...

在终端上,您将看到发送到端点的任何原始数据。例如:

POST /foo/foo HTTP/1.1
Accept: application/json
Connection: keep-alive
Host: example.com
Accept-Language: en-en
Authorization: Bearer ay...
Content-Length: 15
Content-Type: application/json

{"test": false}
Run Code Online (Sandbox Code Playgroud)

  • 对于 gnu-netcat,我必须使用“nc -l -p 1234”。 (2认同)

小智 10

没有日志系统可以完全工作,(无论如何,从请求 2.26 开始,非常旧的版本可能有另一种行为)

\n

好的解决方案是使用“钩子”并在发生时打印详细信息。

\n

这在这里得到了很好的解释:https ://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/

\n

在“打印所有内容”下,

\n

但万一链接失效了,这里是重要的部分

\n
import requests\nfrom requests_toolbelt.utils import dump\n\ndef logging_hook(response, *args, **kwargs):\n    data = dump.dump_all(response)\n    print(data.decode(\'utf-8\'))\n\nhttp = requests.Session()\nhttp.hooks["response"] = [logging_hook]\n\nhttp.get("https://api.openaq.org/v1/cities", params={"country": "BA"})\n
Run Code Online (Sandbox Code Playgroud)\n

这次的结果将是发送的查询和接收的响应的完整跟踪。

\n

我已经通过 POST 和大量标头成功尝试过:它有效。\n不要忘记 pip install requests_toolbelt。

\n
# Output\n< GET /v1/cities?country=BA HTTP/1.1\n< Host: api.openaq.org\n\n> HTTP/1.1 200 OK\n> Content-Type: application/json; charset=utf-8\n> Transfer-Encoding: chunked\n> Connection: keep-alive\n>\n{\n   "meta":{\n      "name":"openaq-api",\n      "license":"CC BY 4.0",\n      "website":"https://docs.openaq.org/",\n      "page":1,\n      "limit":100,\n      "found":1\n   },\n   "results":[\n      {\n         "country":"BA",\n         "name":"Gora\xc5\xbede",\n         "city":"Gora\xc5\xbede",\n         "count":70797,\n         "locations":1\n      }\n   ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n


Tim*_*rry 9

您可以使用HTTP Toolkit来完成此操作。

如果您需要快速执行此操作而无需更改代码,它尤其有用:您可以从 HTTP Toolkit 打开终端,照常从那里运行任何 Python 代码,您将能够看到每个 HTTP/HTTPS 的完整内容立即请求。

有一个免费版本可以做你需要的一切,而且它是 100% 开源的。

我是 HTTP Toolkit 的创建者;实际上,我自己构建了它来为我解决完全相同的问题!我也试图调试支付集成,但他们的 SDK 不起作用,我不知道为什么,我需要知道实际发生了什么才能正确修复它。这非常令人沮丧,但能够看到原始流量确实有帮助。

  • 很棒的工作伙伴!经过几个小时的测试和摆弄提琴手失败后,这对我帮助很大...... (2认同)

Aar*_*oin 7

之前的答案似乎被否决了,因为它以“没有什么完全有效”开始,然后提供了这个完美的解决方案:

  1. 使用 来安装requests_toolbelt实用程序集合pip install requests-toolbelt
  2. 像这样使用它:
    import requests
    from requests_toolbelt.utils import dump
    
    response = requests.get("https://v2.jokeapi.dev/joke/Any?safe-mode")
    print(dump.dump_all(response).decode("utf-8"))
    
    Run Code Online (Sandbox Code Playgroud)


Kaf*_*nek 5

如果您使用的是Python 2.x,请尝试安装urllib2开启工具.这应该打印出你的标题,虽然你可能必须将它与你用来点击HTTPS的其他开启者结合起来.

import urllib2
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1)))
urllib2.urlopen(url)
Run Code Online (Sandbox Code Playgroud)