Python httplib响应

2024-03-29 15:13:18 发布

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

我正在使用python为elgg编写REST客户机,即使请求成功,我也会得到以下响应:

Traceback (most recent call last):
  File "testclient.py", line 94, in <module>
    result = sendMessage(token, h1)
  File "testclient.py", line 46, in sendMessage
    res = h1.getresponse().read()
  File "C:\Python25\lib\httplib.py", line 918, in getresponse
    raise ResponseNotReady()
httplib.ResponseNotReady

看标题,我看到了('content-length','5749'),所以我知道那里有一个页面,但是我不能使用.read()来查看它,因为出现了异常。responseNoteady是什么意思?为什么我看不到返回的内容?


Tags: inpyrestread客户机lineh1httplib
3条回答

我今天遇到了同样的异常,使用以下代码:

    conn = httplib.HTTPConnection(self._host, self._port)
    conn.putrequest('GET',
        '/retrieve?id={0}'.format(parsed_store_response['id']))
    retr_response = conn.getresponse()

我没有注意到我使用的是putrequest,而不是request;我在混合接口。ResponseNotReady被引发,因为我还没有实际发送请求。

确保不要重用以前连接中的同一对象。一旦服务器keep alive结束并且套接字关闭,您将点击此按钮。

前面的答案是正确的,但还有另一种情况可能会出现这种异常:

在不完全读取任何中间响应的情况下发出多个请求。

例如:

conn.request('PUT',...)
conn.request('GET',...)
# will not work: raises ResponseNotReady

conn.request('PUT',...)
r = conn.getresponse()
r.read() # <-- that's the important call!
conn.request('GET',...)
r = conn.getresponse()
r.read() # <-- same thing

等等。

相关问题 更多 >