Note that there are some explanatory texts on larger screens.

plurals
  1. POConstipated Python urllib2 sockets
    primarykey
    data
    text
    <p>I've been scouring the Internet looking for a solution to my problem with Python. I'm trying to use a urllib2 connection to read a potentially endless stream of data from an HTTP server. It's part of some interactive communication, so it's important that I can get the data that's available, even if it's not a whole buffer full. There seems to be no way to have <code>read</code> \ <code>readline</code> return the <em>available</em> data. It will block forever waiting for the entire (endless) stream before it returns. </p> <p>Even if I set the underlying file descriptor to non-blocking using fnctl, the urllib2 file-object <em>still blocks!!</em> In general there seems to be no way to make python file-objects, upon <code>read</code>, return all available data if there is some and block otherwise. </p> <p>I've seen a few posts about people seeking help with this, but I have seen no solutions. What gives? Am I missing something? This seems like such a normal use-case to completely ruin! I'm hoping to utilize urllib2's ability to detect configured proxies and use chunked encoding, but I can't if it won't cooperate.</p> <p>Edit: Upon request, here is some example code</p> <p>Client:</p> <pre><code>connection = urllib2.urlopen(commandpath) id = connection.readline() </code></pre> <p>Now suppose that the server is using chunked transfer encoding, and writes one chunk down the stream and the chunk contains the line, and then waits. The connection is still open, but the client has data waiting in a buffer.</p> <p>I cannot get <code>read</code> or <code>readline</code> to return the data I know it has waiting for it, because it tries to read until the end of the connection. In this case the connection may never close so it will wait either <em>forever</em> or until an inactivity timeout occurs, severing the connection. Once the connection is severed it will return, but that's obviously not the behavior I want.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload