Python httplib ResponseNotReady

前端 未结 6 555
南笙
南笙 2020-12-04 23:59

I\'m writing a REST client for elgg using python, and even when the request succeeds, I get this in response:

Traceback (most recent call last):
  File \"tes         


        
相关标签:
6条回答
  • Unable to add comment to @Bokeh 's answer; as I do not have the requisite reputation yet on this platform.

    So, adding as answer: Bokeh's answer worked for me.

    I was trying to pipeline multiple requests sequentially over the same connection object. For few of the responses I wanted to process the response later, hence missed to read the response.

    From my experience, I second Bokeh's answer:

    response.read() is a must after each request. Even if you wish to process response or not.

    From my standpoint this question would have been incomplete without Bokeh's answer. Thanks @Bokeh

    0 讨论(0)
  • 2020-12-05 00:24

    I was running into this same exception today, using this code:

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

    I didn't notice that I was using putrequest rather than request; I was mixing my interfaces. ResponseNotReady is raised because I haven't actually sent the request yet.

    0 讨论(0)
  • 2020-12-05 00:27

    This can also occur if a firewall blocks the connection.

    0 讨论(0)
  • 2020-12-05 00:28

    Make sure you don't reuse the same object from a previous connection. You will hit this once the server keep-alive ends and the socket closes.

    0 讨论(0)
  • 2020-12-05 00:34

    Previous answers are correct, but there's another case where you could get that exception:

    Making multiple requests without reading any intermediate responses completely.

    For instance:

    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
    

    and so on.

    0 讨论(0)
  • 2020-12-05 00:43

    Additionally, errors like this can occur when the server sends a response without a Content-Length header, which will cripple the state of the HTTP client if Keep-Alive is used and another request is sent over the same socket.

    0 讨论(0)
提交回复
热议问题