记录来自python-requests模块的所有请求
问题内容:
我正在使用python Requests。我需要调试一些OAuth
活动,为此,我希望它记录正在执行的所有请求。我可以通过获得此信息ngrep
,但是很遗憾,无法grep
https连接(这是所需的OAuth
)
如何激活Requests
正在访问的所有URL(+参数)的日志记录?
问题答案:
基础urllib3
库记录与logging
模块(而不是POST
主体)的所有新连接和URL
。对于GET
请求,这应该足够了:
import logging
logging.basicConfig(level=logging.DEBUG)
这为您提供了最详细的日志记录选项;有关如何配置日志记录级别和目标的更多详细信息,请参见日志记录操作指南。
简短的演示:
>>> import requests
>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> r = requests.get('http://httpbin.org/get?foo=bar&baz=python')
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org:80
DEBUG:urllib3.connectionpool:http://httpbin.org:80 "GET /get?foo=bar&baz=python HTTP/1.1" 200 366
根据urllib3的确切版本,将记录以下消息:
INFO
:重定向WARN
:连接池已满(如果发生这种情况,通常会增加连接池的大小)WARN
:无法解析标头(格式无效的响应标头)WARN
:重试连接WARN
:证书与预期的主机名不匹配WARN
:处理分块响应时,接收到的内容长度和传输编码都包含响应DEBUG
:新连接(HTTP或HTTPS)DEBUG
:连接断开DEBUG
:连接详细信息:方法,路径,HTTP版本,状态代码和响应长度DEBUG
:重试计数增量
这不包括标题或正文。urllib3
使用http.client.HTTPConnection
该类完成任务,但是该类不支持日志记录,通常只能将其配置为
打印 到stdout。但是,您可以绑定该模块,以将所有调试信息发送到日志记录,而无需print
在该模块中引入其他名称:
import logging
import http.client
httpclient_logger = logging.getLogger("http.client")
def httpclient_logging_patch(level=logging.DEBUG):
"""Enable HTTPConnection debug logging to the logging framework"""
def httpclient_log(*args):
httpclient_logger.log(level, " ".join(args))
# mask the print() built-in in the http.client module to use
# logging instead
http.client.print = httpclient_log
# enable debugging
http.client.HTTPConnection.debuglevel = 1
调用httpclient_logging_patch()
导致http.client
连接将所有调试信息输出到标准记录器,因此被以下对象拾取logging.basicConfig()
:
>>> httpclient_logging_patch()
>>> r = requests.get('http://httpbin.org/get?foo=bar&baz=python')
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org:80
DEBUG:http.client:send: b'GET /get?foo=bar&baz=python HTTP/1.1\r\nHost: httpbin.org\r\nUser-Agent: python-requests/2.22.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\n\r\n'
DEBUG:http.client:reply: 'HTTP/1.1 200 OK\r\n'
DEBUG:http.client:header: Date: Tue, 04 Feb 2020 13:36:53 GMT
DEBUG:http.client:header: Content-Type: application/json
DEBUG:http.client:header: Content-Length: 366
DEBUG:http.client:header: Connection: keep-alive
DEBUG:http.client:header: Server: gunicorn/19.9.0
DEBUG:http.client:header: Access-Control-Allow-Origin: *
DEBUG:http.client:header: Access-Control-Allow-Credentials: true
DEBUG:urllib3.connectionpool:http://httpbin.org:80 "GET /get?foo=bar&baz=python HTTP/1.1" 200 366