所以我从python脚本向HTTP服务器发送一些JSON数据。
connection.request('PUT', '/characteristics', json_put_data)
resp3 = connection.getresponse()
print(resp3.read().decode())
服务器正在回复以下内容:
"HTTP/1.1 204 No Content\r\nContent-type: json\r\n\r\n"
不知何故,我收到了一个python错误
Traceback (most recent call last):
File "test.py", line 28, in <module>
resp3 = connection.getresponse()
File "C:\Python34\lib\http\client.py", line 1172, in getresponse
response.begin()
File "C:\Python34\lib\http\client.py", line 351, in begin
version, status, reason = self._read_status()
File "C:\Python34\lib\http\client.py", line 321, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: ''
我的204响应是否格式不正确,或者你不能在204 No Content响应上调用read()。decode(),因为标题后面没有技术上的数据?我可以调整(只要它仍然确认HTTP REST标准),因为我正在编程服务器和客户端。感谢。
编辑:我已经确定问题出在PUT之后的后续 HTTP调用中。无论下一个调用是HTTP GET还是PUT,它都会失败并且我上面粘贴了相同的跟踪。这只能在发送PUT后重现(我可以根据需要制作尽可能多的GET请求)。我开始怀疑我的204 No内容格式不正确,因为我们可以从wireshark跟踪中看到它期待另一个http响应?
Hypertext Transfer Protocol
HTTP/1.1 204 No Content\r\n
[Expert Info (Chat/Sequence): HTTP/1.1 204 No Content\r\n]
Request Version: HTTP/1.1
Status Code: 204
Response Phrase: No Content
Content-type: json\r\n
\r\n
**[HTTP response 1/2]**
[Time since request: 0.000764000 seconds]
[Request in frame: 4]
[Next request in frame: 14]