Dun*_*ger 5 python python-requests
收到多部分响应时出现错误。
WARNING connectionpool Failed to parse headers (url=************): [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 399, in _make_request
assert_header_parsing(httplib_response.msg)
File "/usr/local/lib/python3.6/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)
这是否意味着该库不支持多部分响应?我服务器的响应在所有其他情况下都适用,包括对浏览器的响应,因此我有些困惑。
有任何想法吗?
这是从服务器返回的内容(为简洁起见,正文被截断了):
HTTP/1.1 200 OK
X-Powered-By: Servlet/3.1
X-CA-Affinity: 2411441258
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Encoding: gzip
X-Compressed-By: BICompressionFilter
Content-Type: multipart/related; type="text/xml"; boundary="1521336443366.-7832488688540884419.-1425166373"
Content-Language: en-US
Transfer-Encoding: chunked
Date: Sun, 18 Mar 2018 01:27:23 GMT
a
154e
<i ? O x\?L dre Qyi
/su k
Run Code Online (Sandbox Code Playgroud)
当然,这是编码的。如果我在Fiddler中对其进行解码,则结果如下所示:
HTTP/1.1 200 OK
X-Powered-By: Servlet/3.1
X-CA-Affinity: 2411441258
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
X-Compressed-By: BICompressionFilter
Content-Type: multipart/related; type="text/xml"; boundary="1521336443366.-7832488688540884419.-1425166373"
Content-Language: en-US
Date: Sun, 18 Mar 2018 01:27:23 GMT
Content-Length: 17419
--1521336443366.-7832488688540884419.-1425166373
Content-Type: text/xml; charset=utf-8
Content-Length: 15261
<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)
回答您的问题:是的,Requests 可以很好地处理多部分请求。话虽如此,我也看到了你遇到的同样的错误。
这似乎是 urllib3 中的一个错误,但可能与 python 附带的 httplib 包一样严重。在你的情况下,我猜它会回到响应的 UTF-8 编码,显然你对此无能为力(除非你也维护服务器端)。我相信忽略它是完全安全的,但简单地包含urllib3.disable_warnings()似乎对我来说并不起作用。如果您想消除此特定警告,可以在代码中包含日志过滤器。(这种方法归功于家庭助理维护人员)
def filter_urllib3_logging():
"""Filter header errors from urllib3 due to a urllib3 bug."""
urllib3_logger = logging.getLogger("urllib3.connectionpool")
if not any(isinstance(x, NoHeaderErrorFilter)
for x in urllib3_logger.filters):
urllib3_logger.addFilter(
NoHeaderErrorFilter()
)
class NoHeaderErrorFilter(logging.Filter):
"""Filter out urllib3 Header Parsing Errors due to a urllib3 bug."""
def filter(self, record):
"""Filter out Header Parsing Errors."""
return "Failed to parse headers" not in record.getMessage()
Run Code Online (Sandbox Code Playgroud)
然后,只需调用filter_urllib3_logging()您的设置即可。它不会停止警告,但会隐藏它们:D
!!请注意!!这也将隐藏,因此,很难诊断由解析标头引起的任何错误,这些错误有时可能是合法的错误!
| 归档时间: |
|
| 查看次数: |
759 次 |
| 最近记录: |