基础
urllib3库记录与
logging模块(而不是
POST主体)的所有新连接和URL
。对于
GET请求,这应该足够了:
import logginglogging.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:80DEBUG: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。但是,您可以绑定该模块,以将所有调试信息发送到日志记录,而无需
import loggingimport http.clienthttpclient_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:80DEBUG:http.client:send: b'GET /get?foo=bar&baz=python HTTP/1.1rnHost: httpbin.orgrnUser-Agent: python-requests/2.22.0rnAccept-Encoding: gzip, deflaternAccept: */*rnConnection: keep-alivernrn'DEBUG:http.client:reply: 'HTTP/1.1 200 OKrn'DEBUG:http.client:header: Date: Tue, 04 Feb 2020 13:36:53 GMTDEBUG:http.client:header: Content-Type: application/jsonDEBUG:http.client:header: Content-Length: 366DEBUG:http.client:header: Connection: keep-aliveDEBUG:http.client:header: Server: gunicorn/19.9.0DEBUG:http.client:header: Access-Control-Allow-Origin: *DEBUG:http.client:header: Access-Control-Allow-Credentials: trueDEBUG:urllib3.connectionpool:http://httpbin.org:80 "GET /get?foo=bar&baz=python HTTP/1.1" 200 366


