Answers:
一种简单的方法:启用登录请求的最新版本(1.x及更高版本)。
请求用途http.client
和logging
模块配置到控制日志记录级别,如所描述这里。
从链接文档中摘录的代码:
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')
$ 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
import httplib
为import requests.packages.urllib3.connectionpool as httplib
或使用6和from six.moves import http_client as httplib
。
requests
2.18.1和Python 3中,记录器logging.getLogger("requests.packages.urllib3")
不存在或无效。
from http.client import HTTPConnection
r = requests.get('https://api.github.com', auth=('user', 'pass'))
r
是回应。它具有一个request属性,其中包含您需要的信息。
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
r.request.headers
给出标题:
{'Accept': '*/*',
'Accept-Encoding': 'identity, deflate, compress, gzip',
'Authorization': u'Basic dXNlcjpwYXNz',
'User-Agent': 'python-requests/0.12.1'}
然后r.request.data
将主体作为映射。urllib.urlencode
如果他们愿意,可以将其转换为:
import urllib
b = r.request.data
encoded_body = urllib.urlencode(b)
根据响应的类型,.data
-attribute可能会丢失,而.body
-attribute会在那里。
response.request
看来,这似乎是一个音符PreparedRequest
。它没有.data
,.body
而是。
response.url
(有点不同,因为它不是response.request...
您可以使用HTTP Toolkit来做到这一点。
如果您需要在不更改代码的情况下快速执行此操作,则特别有用:您可以从HTTP Toolkit打开终端,从那里正常运行任何Python代码,并且可以查看每个HTTP / HTTPS的全部内容。立即提出要求。
有一个免费版本可以满足您的所有需求,并且它是100%开放源代码。
我是HTTP Toolkit的创建者;我实际上是自己构建的,以便不久前为我解决完全相同的问题!我也曾尝试调试付款集成,但他们的SDK无法正常工作,我无法告知原因,我需要知道实际情况如何对其进行正确修复。这非常令人沮丧,但是能够看到原始流量确实有所帮助。