Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to loop over an httplib.HTTPResponse's data?
    primarykey
    data
    text
    <p>I'm trying to develop a very simple proof-of-concept to retrieve and process data in a streaming manner. The server I'm requesting from will send data in chunks, which is good, but I'm having issues using <code>httplib</code> to iterate through the chunks.</p> <p>Here's what I'm trying: import httplib</p> <pre><code>def getData(src): d = src.read(1024) while d and len(d) &gt; 0: yield d d = src.read(1024) if __name__ == "__main__": con = httplib.HTTPSConnection('example.com', port='8443', cert_file='...', key_file='...') con.putrequest('GET', '/path/to/resource') response = con.getresponse() for s in getData(response): print s raw_input() # Just to give me a moment to examine each packet </code></pre> <p>Pretty simple. Just open an HTTPS connection to server, request a resource, and grab the result, 1024 bytes at a time. I'm definitely making the HTTPS connection successfully, so that's not a problem at all.</p> <p>However, what I'm finding is that the call to <code>src.read(1024)</code> returns the same thing every time. It only ever returns the first 1024 bytes of the response, apparently never keeping track of a cursor within the file.</p> <p>So how am I supposed to receive 1024 bytes at a time? The documentation on <code>read()</code> is pretty sparse. I've thought about using urllib or urllib2, but neither seems to be able to make an HTTPS connection.</p> <p>HTTPS is required, and I am working in a rather restricted corporate environment where packages like <a href="http://docs.python-requests.org/en/latest/" rel="nofollow">Requests</a> are a bit tough to get my hands on. If possible, I'd like to find a solution within Python's standard lib.</p> <h1>// Big Old Fat Edit</h1> <p>Turns out in my original code I had simply forgot to update the <code>d</code> variable. I initialized it with a read outside the <code>yield</code> loop and never changed it in the loop. Once I added it back in there it worked perfectly.</p> <p>So, in short, I'm just a big idiot.</p>
    singulars
    1. This table or related slice is empty.
    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