Python httplib ResponseNotReady

43,236

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
43,236

Related videos on Youtube

directedition
Author by

directedition

Updated on January 09, 2021

Comments

  • directedition
    directedition almost 3 years

    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 "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
    

    Looking at the header, I see ('content-length', '5749'), so I know there is a page there, but I can't use .read() to see it because the exception comes up. What does ResponseNotReady mean and why can't I see the content that was returned?

    • ChristopheD
      ChristopheD over 13 years
      Are you re-using the connection?
    • directedition
      directedition over 13 years
      Indeed. Oddly, sometimes it works and sometimes it doesn't. I can't determine what behavior determines that though.
  • rossipedia
    rossipedia over 13 years
    Generally, as a rule, I don't bother trying to reuse HttpRequest objects unless I have a specific, performance-driven need to do so. Just single-shot 'em
  • Nathan Schwermann
    Nathan Schwermann over 9 years
    Thats what I was needing .read() so I can reuse my connection.
  • Dex
    Dex about 8 years
    In my case, adding preload_content=False solved the issue. Here is the snippet: http = urllib3.PoolManager(threadsNo, maxsize=threadsNo, block=True); request = http.request('GET', queryUrl, preload_content=False) It looks like request.release_conn() does not really release the previous connection object unless above parameter is passed to http.request. More details here: urllib3.readthedocs.org/en/latest/pools.html
  • Neil
    Neil over 7 years
    is .read() expensive. What if I don't care about the response? Can I make it run faster ?>
  • mvsagar
    mvsagar over 7 years
    @ronnefeldt, Python docs at docs.python.org/3/library/http.client.html has the following note: "Note that you must have read the whole response before you can send a new request to the server.".
  • Daniel Cull
    Daniel Cull over 5 years
    If you hadn't said this I would have had a very upsetting next few hours maybe even days. THANK YOU