Python http.client仅在1个系统上在完全有效的HTTP上抛出BadStatusLine。 Wireshark已验证

时间:2018-11-08 21:50:59

标签: python http

在我所有的系统上,以下HTTP请求都会给我以下响应(不是HTTPS,API密钥经过审查):

GET /ShippingAPI.dll?API=RateV4&XML=%3CRateV4Request+USERID%3D%22xxxxxxx%22%3E%0A%09%09%09%3CPackage+ID%3D%221%22%3E%0A%09%09%09%09%3CService%3EALL%3C%2FService%3E%0A%09%09%09%09%3CZipOrigination%3E71343%3C%2FZipOrigination%3E%0A%09%09%09%09%3CZipDestination%3E98501%3C%2FZipDestination%3E%0A%09%09%09%09%3CPounds%3E5%3C%2FPounds%3E%0A%09%09%09%09%3COunces%3E0%3C%2FOunces%3E%0A%09%09%09%09%3CContainer%3EVARIABLE%3C%2FContainer%3E%0A%09%09%09%09%3CSize%3EREGULAR%3C%2FSize%3E%0A%09%09%09%09%3CWidth%3E7%3C%2FWidth%3E%0A%09%09%09%09%3CLength%3E7%3C%2FLength%3E%0A%09%09%09%09%3CHeight%3E6%3C%2FHeight%3E%0A%09%09%09%09%3CGirth%3E33%3C%2FGirth%3E%0A%09%09%09%09%3CMachinable%3Efalse%3C%2FMachinable%3E%0A%09%09%09%3C%2FPackage%3E%0A%09%09%3C%2FRateV4Request%3E HTTP/1.1
Host: production.shippingapis.com
Accept-Encoding: identity

响应:

HTTP/1.0 200 OK
X-Backside-Transport: OK OK
Cache-Control: private
Content-Type: text/xml
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 08 Nov 2018 21:26:14 GMT
X-Global-Transaction-ID: 0bee6b0a5be4a9f7ea895831
Access-Control-Allow-Origin: *
Connection: Keep-Alive
Content-Length: 1070
X-FRAME-OPTIONS: SAMEORIGIN

<?xml version="1.0" encoding="UTF-8"?>
<RateV4Response><Package ID="1"><ZipOrigination>713...

此信息来自Wireshark。

我到处都运行相同的Python版本,即3.6.1 32b,并随pipenv安装了软件包,因此它们完全相同。

为什么一台计算机在此行阻塞:

Connection: Keep-Alive

踪迹:

Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
File "<string>", line 2, in raise_from
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1331, in getresponse
    response.begin()
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 279, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: Connection: Keep-Alive

1 个答案:

答案 0 :(得分:1)

剧情:这是恶意软件。 Wireshark 显示了实际服务器的HTTP响应,该响应以 HTTP / 1.1 200 OK 开始。但是,这不是Python的 http.client 看到的。

我从https://github.com/python/cpython/blob/3.6/Lib/http/client.py下载了 http.client 源,运行它,然后使用调试器进入它。它看到的HTTP响应以 HTTP / 1.1 100 CONTINUE 开始。 Wireshark给我展示了一件事,而http.client给我展示了另一件事。

要将 http.client 的数据记录到stdout,即使使用 Requests ,也要使用此命令:

http.client.HTTPConnection.debuglevel = 1

这种症状可能只意味着一件事:Wireshark和应用程序层之间的HTTP通信混乱。我安装了Malwarebytes并发现:

Type: Potentially Unwanted Program (PUP)
Name: PUP.Optional.Winsock.WnskRST
Path: C:\Windows\Provider.dll

在删除了该文件和一堆其他废话,并重新启动了几次之后,http.client现在很高兴,并且看到了HTTP / 1.1 200 OK标头,应该是这样。