Note that there are some explanatory texts on larger screens.

plurals
  1. POurllib2 write to upstream
    primarykey
    data
    text
    <p>I want to upload (large) random content to an HTTP endpoint. I create it, send it and it's never used again. So I'd like to skip saving it in a temporary file and using <code>{ 'file':open() }</code> as I also need to use form-urlencoded (instead of multipart).<br> I was using pycurl and its READFUNCTION option, but I don't want to depend on pycurl. </p> <p>Is it possible to achieve this with <em>urllib2</em>?</p> <p>I have this code:</p> <pre><code>def upload(): url = 'http://localhost:9999/' values = {'name' : 'Michael Foord', 'location' : 'Northampton', 'language' : open(file).read() } data = urllib.urlencode(values) req = urllib2.Request(url, data) response = urllib2.urlopen(req) the_page = response.read() </code></pre> <p>But as it stands, the whole file will be read when <em>values</em> is defined.<br> So if <em>file</em> was 1Gb of size, this wouldn't work very well (unless I misunderstood how it works). Also, I'd prefer not having to create the random-filled 1Gb file.</p> <hr> <p>Ok, I can subclass <em>StringIO</em> as Daniel mentioned and create the random data in <em>read()</em>, but how do I make <em>urllib2</em> ask for <em>read()</em> chunks <em>while</em> uploading instead of having everything in memory at once?</p> <hr> <p>In an attempt to be more specific, what I'd really like is something like <code>urlConnectionInstance.getOutputStream()</code> in Java that I can then write to. Or some way to tell <em>urllib2</em> to read from my own stream <em>while</em> it is actually dumping the data in the upload stream..</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