<div dir="ltr">Thanks Kent!<br>Here's what I got, works pretty well. :)<br>import urllib2<br><br><br>#functions<br>def reportDownloadProgress(blocknum, bs, size):<br> percent = int(blocknum*bs*100/size)<br> print str(blocknum*bs ) + '/' + str(size) + 'downloaded| ' + str(percent) + '%'<br>
<br>def httpDownload(url, filename, headers=None, reporthook=None, postData=None):<br> reqObj = urllib2.Request(url, postData, headers)<br> fp = urllib2.urlopen(reqObj)<br> headers = <a href="http://fp.info">fp.info</a>()<br>
## This function returns a file-like object with two additional methods:<br> ##<br> ## * geturl() -- return the URL of the resource retrieved<br> ## * info() -- return the meta-information of the page, as a dictionary-like object<br>
##<br> ##Raises URLError on errors.<br> ##<br> ##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> #read & write fileObj to filename<br> tfp = open(filename, 'wb')<br> result = filename, headers<br> bs = 1024*8<br> size = -1<br> read = 0<br> blocknum = 0<br> <br> if reporthook:<br>
if "content-length" in headers:<br> size = int(headers["Content-Length"])<br> reporthook(blocknum, bs, size)<br> <br> while 1:<br> block = fp.read(bs)<br> if block == "":<br>
break<br> read += len(block)<br> tfp.write(block)<br> blocknum += 1<br> if reporthook:<br> reporthook(blocknum, bs, size)<br> <br> fp.close()<br> tfp.close()<br>
del fp<br> del tfp<br><br> # raise exception if actual size does not match content-length header<br> if size >= 0 and read < size:<br> raise ContentTooShortError("retrieval incomplete: got only %i out "<br>
"of %i bytes" % (read, size), result)<br><br> return result<br> <br> <br> <br>url = '<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>'<br>
headers = {<br>'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',<br>'Accept' :<br>'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',<br>
'Accept-Language' : 'fr-fr,en-us;q=0.7,en;q=0.3',<br>'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'<br>}<br><br><br>#test it<br>httpDownload(url, 'testDownload.flv', headers, reportDownloadProgress)<br>
<br><div class="gmail_quote">On Sun, Aug 10, 2008 at 6:42 PM, Kent Johnson <span dir="ltr"><<a href="mailto:kent37@tds.net">kent37@tds.net</a>></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 <<a href="mailto:xboxmuncher@gmail.com">xboxmuncher@gmail.com</a>> wrote:<br>
> I want to download a file via HTTP GET protocol (perhaps HTTP POST in the<br>
> future, whatever method should allow relativly easy changes to be made to<br>
> use POST later on)<br>
> I need to send custom headers like in urllib2.urlrequest()<br>
> I need to be able to monitor the download progress WHILE it downloads like<br>
> the hook function in urllib<br>
><br>
> I looked into urllib2 and it did everything except allow me to check the<br>
> progress of the download during the download, it only downloads the file<br>
> first with urlopen(). I also tried urllib and the reporthook function is<br>
> great, except I can't send custom headers! I wish I could send a REQ object<br>
> to the urlretrieve() function in urllib that way I can prepare those custom<br>
> headers.. :(<br>
><br>
</div><div class="Ih2E3d">> Anyways, i would appreciate if soemone could write me a quick example to do<br>
> this, or point me to the right library/funcs to do it. I want to be able to<br>
> do it in native python libraries.<br>
<br>
</div>urllib2.urlopen() doesn'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>