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

标签 python logging python-requests

我正在使用 python Requests .我需要调试一些 OAuth 事件,为此我希望它记录所有正在执行的请求。我可以使用 ngrep 获取此信息,但遗憾的是无法 grep https 连接(OAuth 需要这些连接)

如何激活对 Requests 正在访问的所有 URL(+ 参数)的记录?

最佳答案

您需要在 httplib 级别启用调试(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]>

您将看到包括 HEADERS 和 DATA 的 REQUEST,以及带有 HEADERS 但不带 DATA 的 RESPONSE。唯一缺少的是未记录的 response.body。

Source

关于python - 记录来自 python-requests 模块的所有请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16337511/

相关文章:

python - 生成器中的程序控制流程是怎样的?

python - DRF : only Author can create or update the book permission

python - 如何使用discord py连续编辑你的昵称

django - 如何创建涉及使用 @property 方法进行计算的 POST 方法

python - Django '/' 只有主页 url 错误

python - Flask应用程序错误: failed to load external entity.无法输入xml文件进行解析

linux - 从服务器删除 TTY_00000000.log 日志

linux - 将文件从 linux vps 上传到虚拟主机

ruby - 瘦服务器多次记录同一个请求

python - 如何使用python web Scraping避免 "Please Verify you are a Human"?