Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我怎样才能看到整个HTTP请求';我的Python应用程序正在发送什么?_Python_Debugging_Https_Python Requests - Fatal编程技术网

我怎样才能看到整个HTTP请求';我的Python应用程序正在发送什么?

我怎样才能看到整个HTTP请求';我的Python应用程序正在发送什么?,python,debugging,https,python-requests,Python,Debugging,Https,Python Requests,在我的例子中,我使用请求库通过HTTPS调用PayPal的API。不幸的是,我从PayPal收到了一个错误,PayPal支持部门无法确定错误是什么或是什么原因造成的。他们希望我“请提供整个请求,包括标题” 我该怎么做 r = requests.get('https://api.github.com', auth=('user', 'pass')) r是一个响应。它有一个请求属性,该属性包含您需要的信息 r.request.allow_redirects r.request.headers

在我的例子中,我使用
请求
库通过HTTPS调用PayPal的API。不幸的是,我从PayPal收到了一个错误,PayPal支持部门无法确定错误是什么或是什么原因造成的。他们希望我“请提供整个请求,包括标题”

我该怎么做

r = requests.get('https://api.github.com', auth=('user', 'pass'))
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
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
-属性可能会丢失,而
.body
-属性可能会出现。

如果您使用的是Python 2.x,请尝试安装一个opener。这应该打印出你的标题,尽管你可能需要将它与你用来点击HTTPS的其他开场白结合起来

import urllib2
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1)))
urllib2.urlopen(url)

verbose
配置选项可能允许您查看所需内容。有


注意:阅读下面的评论:详细配置选项似乎不再可用。

一个简单的方法:在最新版本的请求(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')
示例输出 你可以用它来做这件事

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

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


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

其中哪一个给了我“整个请求,包括标题”?添加了更多内容。除了头部和身体,你还需要什么?我不完全确定他们在找什么。我希望能以精确的格式,逐字节地为他们捕捉到所有经过网络的信息。在我的例子中,这是最好的方式。只有一个注意事项:
响应.request
在我的情况下似乎是一个
PreparedRequest
;它没有
.data
,而是
.body
。对于完整的URL(带有querystring参数),您也可以使用
response.URL
(这有点不同,因为它不是
response.request…
响应。有吗?找不到它。@badcat.here.似乎是。啊,这可以解释。:)仍然,现在这个问题又有点正确了,因为我找不到一种方法来打印服务器和客户端之间的全部通信量以进行调试。是否有一种推荐的“新方法”来实现与详细日志记录相同的效果?演示了请求1.x及更高版本的正确方法。谢谢,@EmmettJ.Butler=),尽管我不确定在最初查询时是否提供了此信息。请注意,httplib在Python 3上不可用。要使代码可移植,请将
import-httplib
替换为
import-requests.packages.urllib3.connectionpool-as-httplib
,或使用six和
from-six.moves将http_客户端作为httplib
导入。在
requests
2.18.1和Python 3中,记录器
logging.getLogger(“requests.packages.urlib3”)
不存在或无效。对于Python3,请参见此处-
来自http.client import-HTTPConnection
不幸的是,“发送:”回复:”和“标题:”行实际上没有记录,只是打印到stdout。但是我想在日志文件中有这些信息!
$ 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