<div dir="ltr">Thanks Kent!<br>Here&#39;s what I got, works pretty well. :)<br>import urllib2<br><br><br>#functions<br>def reportDownloadProgress(blocknum, bs, size):<br>&nbsp;&nbsp;&nbsp; percent = int(blocknum*bs*100/size)<br>&nbsp;&nbsp;&nbsp; print str(blocknum*bs ) + &#39;/&#39; + str(size) + &#39;downloaded| &#39; + str(percent) + &#39;%&#39;<br>
&nbsp;&nbsp;&nbsp; <br>def httpDownload(url, filename, headers=None, reporthook=None, postData=None):<br>&nbsp;&nbsp;&nbsp; reqObj = urllib2.Request(url, postData, headers)<br>&nbsp;&nbsp;&nbsp; fp = urllib2.urlopen(reqObj)<br>&nbsp;&nbsp;&nbsp; headers = <a href="http://fp.info">fp.info</a>()<br>
&nbsp;&nbsp;&nbsp; ##&nbsp;&nbsp;&nbsp; This function returns a file-like object with two additional methods:<br>&nbsp;&nbsp;&nbsp; ##<br>&nbsp;&nbsp;&nbsp; ##&nbsp;&nbsp;&nbsp; * geturl() -- return the URL of the resource retrieved<br>&nbsp;&nbsp;&nbsp; ##&nbsp;&nbsp;&nbsp; * info() -- return the meta-information of the page, as a dictionary-like object<br>
&nbsp;&nbsp;&nbsp; ##<br>&nbsp;&nbsp;&nbsp; ##Raises URLError on errors.<br>&nbsp;&nbsp;&nbsp; ##<br>&nbsp;&nbsp;&nbsp; ##Note that None may be returned if no handler handles the request (though the default installed global OpenerDirector uses UnknownHandler to ensure this never happens). <br>
<br>&nbsp;&nbsp;&nbsp; #read &amp; write fileObj to filename<br>&nbsp;&nbsp;&nbsp; tfp = open(filename, &#39;wb&#39;)<br>&nbsp;&nbsp;&nbsp; result = filename, headers<br>&nbsp;&nbsp;&nbsp; bs = 1024*8<br>&nbsp;&nbsp;&nbsp; size = -1<br>&nbsp;&nbsp;&nbsp; read = 0<br>&nbsp;&nbsp;&nbsp; blocknum = 0<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; if reporthook:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if &quot;content-length&quot; in headers:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; size = int(headers[&quot;Content-Length&quot;])<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; reporthook(blocknum, bs, size)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; while 1:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; block = fp.read(bs)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if block == &quot;&quot;:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; read += len(block)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; tfp.write(block)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; blocknum += 1<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if reporthook:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; reporthook(blocknum, bs, size)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; fp.close()<br>&nbsp;&nbsp;&nbsp; tfp.close()<br>
&nbsp;&nbsp;&nbsp; del fp<br>&nbsp;&nbsp;&nbsp; del tfp<br><br>&nbsp;&nbsp;&nbsp; # raise exception if actual size does not match content-length header<br>&nbsp;&nbsp;&nbsp; if size &gt;= 0 and read &lt; size:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; raise ContentTooShortError(&quot;retrieval incomplete: got only %i out &quot;<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &quot;of %i bytes&quot; % (read, size), result)<br><br>&nbsp;&nbsp;&nbsp; return result<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>url = &#39;<a href="http://akvideos.metacafe.com/ItemFiles/%5BFrom%20www.metacafe.com%5D%20292662.2155544.11.flv">http://akvideos.metacafe.com/ItemFiles/%5BFrom%20www.metacafe.com%5D%20292662.2155544.11.flv</a>&#39;<br>
headers = {<br>&#39;User-Agent&#39; : &#39;Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)&#39;,<br>&#39;Accept&#39; :<br>&#39;text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5&#39;,<br>
&#39;Accept-Language&#39; : &#39;fr-fr,en-us;q=0.7,en;q=0.3&#39;,<br>&#39;Accept-Charset&#39; : &#39;ISO-8859-1,utf-8;q=0.7,*;q=0.7&#39;<br>}<br><br><br>#test it<br>httpDownload(url, &#39;testDownload.flv&#39;, headers, reportDownloadProgress)<br>
<br><div class="gmail_quote">On Sun, Aug 10, 2008 at 6:42 PM, Kent Johnson <span dir="ltr">&lt;<a href="mailto:kent37@tds.net">kent37@tds.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">On Sun, Aug 10, 2008 at 6:09 PM, xbmuncher &lt;<a href="mailto:xboxmuncher@gmail.com">xboxmuncher@gmail.com</a>&gt; wrote:<br>
&gt; I want to download a file via HTTP GET protocol (perhaps HTTP POST in the<br>
&gt; future, whatever method should allow relativly easy changes to be made to<br>
&gt; use POST later on)<br>
&gt; I need to send custom headers like in urllib2.urlrequest()<br>
&gt; I need to be able to monitor the download progress WHILE it downloads like<br>
&gt; the hook function in urllib<br>
&gt;<br>
&gt; I looked into urllib2 and it did everything except allow me to check the<br>
&gt; progress of the download during the download, it only downloads the file<br>
&gt; first with urlopen(). I also tried urllib and the reporthook function is<br>
&gt; great, except I can&#39;t send custom headers! I wish I could send a REQ object<br>
&gt; to the urlretrieve() function in urllib that way I can prepare those custom<br>
&gt; headers.. :(<br>
&gt;<br>
</div><div class="Ih2E3d">&gt; Anyways, i would appreciate if soemone could write me a quick example to do<br>
&gt; this, or point me to the right library/funcs to do it. I want to be able to<br>
&gt; do it in native python libraries.<br>
<br>
</div>urllib2.urlopen() doesn&#39;t read the data from the remote site, it<br>
returns a file-like object with a read() method. You can read the data<br>
in chunks and call a reporthook yourself.<br>
<br>
Take a look at the source to URLopener.retrieve() in urllib.py for some hints.<br>
<font color="#888888"><br>
Kent<br>
</font></blockquote></div><br></div>