禁用从 python3 urllib3 出现的 HeaderParsingError

Der*_*Lee 5 python urllib3 python-requests

如何抑制出现在 urllib3 库中的Failed to parse headers 错误

以下错误不断出现:

Failed to parse headers (url=https://test): [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
Traceback (most recent call last):
  File "/opt/test_project/.venv/lib/python3.5/site-packages/urllib3/connectionpool.py", line 399, in _make_request
    assert_header_parsing(httplib_response.msg)
  File "/opt/test_project/.venv/lib/python3.5/site-packages/urllib3/util/response.py", line 66, in assert_header_parsing
    raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
urllib3.exceptions.HeaderParsingError: [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
Run Code Online (Sandbox Code Playgroud)

我试图用

import urllib3
# Disable SSL warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Disbale urllib3.exceptions.HeaderParsingError:
urllib3.disable_warnings(urllib3.exceptions.HeaderParsingError)

Run Code Online (Sandbox Code Playgroud)

但似乎它不起作用,因为它仍在出现。

网上有一些解决方案,但这是通过日志抑制的。我正在寻找一种方法来抑制它而不是从日志级别。

AFAIK,这只是来自 urllib3 的警告,它已被报告为错误。因此,有什么办法可以抑制这种情况吗?

Saf*_* CK 3



使用标准 python 库“ logging ”抑制日志


将此代码放在现有代码的顶部以忽略urllib3包中的日志

import logging
urllib3_logger = logging.getLogger('urllib3')
urllib3_logger.setLevel(logging.CRITICAL)
Run Code Online (Sandbox Code Playgroud)