请求是否正确支持多部分响应?

2024-09-19 22:01:40 发布

您现在位置:Python中文网/ 问答频道 /正文

接收多部分响应时出错。在

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

这是否意味着库不支持多部分响应?服务器的响应在所有其他情况下都有效,包括对浏览器的响应,所以我有点困惑。在

有什么想法吗?在

这是从服务器返回的内容(当然为了简洁起见,正文被截断):

^{pr2}$

当然这是编码的。如果我在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"?>

Tags: pydatalibpackagesusrlocalsitexml
1条回答
网友
1楼 · 发布于 2024-09-19 22:01:40

回答您的问题:是的,请求可以处理多部分请求。我也看到了,你也犯了同样的错误。在

这似乎是urllib3中的一个bug,但可能会深入到python附带的httplib包中。在您的例子中,我想它会返回到响应的UTF-8编码,显然您不能做太多的事情(除非您还维护服务器端)。我相信忽略它是完全安全的,但是简单地包含urllib3.disable_warnings()对我来说似乎没有什么好处。如果要使此特定警告静音,可以在代码中包含日志过滤器。(此方法归功于home-assistant维护者)

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()

然后,只需在设置中调用filter_urllib3_logging()。它没有停止警告,但它确实隐藏了它们:D

!!请注意!!这也会隐藏,因此,很难诊断任何由解析头引起的错误,这些错误有时可能是合法的错误!

相关问题 更多 >