logging来自python-requests模块的所有请求

我正在使用python 请求 。 我需要debugging一些OAuth活动,为此我希望它logging所有正在执行的请求。 我可以通过ngrep获取这些信息,但不幸的是,不能grep https连接(这是OAuth所需的)

如何激活日志loggingRequests正在访问的所有URL(+参数)?

底层urllib3库logging所有新的连接和URL与logging模块 ,但不是POST主体。 对于GET请求,这应该足够了:

 import logging logging.basicConfig(level=logging.DEBUG) 

这给你最详细的日志logging选项; 有关如何configuration日志logging级别和目标的详细信息,请参阅日志loggingHOWTO 。

简短演示:

 >>> import requests >>> import logging >>> logging.basicConfig(level=logging.DEBUG) >>> r = requests.get('http://httpbin.org/get?foo=bar&baz=python') INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org DEBUG:requests.packages.urllib3.connectionpool:"GET /get?foo=bar&baz=python HTTP/1.1" 200 353 

以下消息被logging:

  • INFO :新连接(HTTP或HTTPS)
  • INFO :丢弃的连接
  • INFO :redirect
  • WARN :连接池已满(如果发生这种情况通常会增加连接池的大小)
  • WARN :重试连接
  • DEBUG :连接细节:方法,path,HTTP版本,状态码和响应长度

您需要在httplib级别启用debugging( requestsurllib3httplib )。

这里有一些函数可以同时切换( ..._on()..._off() )或暂时使用它:

 import logging import contextlib try: from http.client import HTTPConnection # py3 except ImportError: from httplib import HTTPConnection # py2 def debug_requests_on(): '''Switches on logging of the requests module.''' HTTPConnection.debuglevel = 1 logging.basicConfig() logging.getLogger().setLevel(logging.DEBUG) requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True def debug_requests_off(): '''Switches off logging of the requests module, might be some side-effects''' HTTPConnection.debuglevel = 0 root_logger = logging.getLogger() root_logger.setLevel(logging.WARNING) root_logger.handlers = [] requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.WARNING) requests_log.propagate = False @contextlib.contextmanager def debug_requests(): '''Use with 'with'!''' debug_requests_on() yield debug_requests_off() 

演示使用:

 >>> requests.get('http://httpbin.org/') <Response [200]> >>> debug_requests_on() >>> requests.get('http://httpbin.org/') INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 12150 send: 'GET / HTTP/1.1\r\nHost: httpbin.org\r\nConnection: keep-alive\r\nAccept- Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.11.1\r\n\r\n' reply: 'HTTP/1.1 200 OK\r\n' header: Server: nginx ... <Response [200]> >>> debug_requests_off() >>> requests.get('http://httpbin.org/') <Response [200]> >>> with debug_requests(): ... requests.get('http://httpbin.org/') INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org ... <Response [200]> 

您将看到REQUEST,包括HEADERS和DATA,以及与HEADERS但没有DATA的RESPONSE。 唯一缺less的将是没有logging的response.body。

资源

对于那些使用python 3+

 import requests import logging import http.client http.client.HTTPConnection.debuglevel = 1 logging.basicConfig() logging.getLogger().setLevel(logging.DEBUG) requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True