【问题标题】:urllib3 debug request headerurllib3 调试请求头
【发布时间】:2015-08-08 10:16:22
【问题描述】:

我正在使用 urllib3,我想查看发送的标头。

我在文档中找到了这个,但它没有打印标题:

urllib3.add_stderr_logger(1)

有什么办法吗?

【问题讨论】:

    标签: python-3.x urllib3


    【解决方案1】:

    目前,实现包含在 urllib3 中发送的标头的真正详细日志记录的最佳方法是覆盖 httplib 中的默认值(内部使用)。

    对于 Python 3:

    # You'll need to do this before urllib3 creates any http connection objects
    import http.client
    http.client.HTTPConnection.debuglevel = 5
    
    # Now you can use urllib3 as normal
    import urllib3
    http = urllib3.PoolManager()
    r = http.request('GET', ...)
    

    在 Python 2 中,HTTPConnection 对象位于 httplib 模块下。

    这将为使用 httplib 的任何内容打开详细日志记录。请注意,这不是使用记录在案的 httplib API,而是猴子修补 HTTPConnection 类的默认值。

    目标是为这类事情添加更好的 urllib3-native 日志记录,但尚未实现。相关问题:https://github.com/shazow/urllib3/issues/107

    【讨论】:

    • 这适用于 Python 2.7。在 Python 3.x 中,您需要执行 import http; http.client.HTTPConnection.debuglevel=5
    • @DanLenski 我正在尝试让您的解决方案与日志文件一起使用,但它仅适用于stdout。更多信息:stackoverflow.com/q/58738195/1090360
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2021-03-30
    • 1970-01-01
    • 2023-04-03
    • 2021-10-22
    相关资源
    最近更新 更多