禁用从python3 urllib3出现的HeaderParsingError

时间:2019-06-26 23:08:50

标签: python python-requests urllib3

如何抑制urllib3库中出现的无法解析标头错误

以下错误不断出现:

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: ''

我尝试使用

禁止显示
import urllib3
# Disable SSL warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Disbale urllib3.exceptions.HeaderParsingError:
urllib3.disable_warnings(urllib3.exceptions.HeaderParsingError)

但是它似乎仍然无法正常工作。

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

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

1 个答案:

答案 0 :(得分:2)



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


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

import logging
urllib3_logger = logging.getLogger('urllib3')
urllib3_logger.setLevel(logging.CRITICAL)