[BangPypers] How to check file size before downloading it

Anand Balachandran Pillai abpillai at gmail.com
Wed May 7 14:11:54 CEST 2008


Make a HEAD request. Here is one way of doing it, using urllib2.

class HeadRequest(urllib2.Request):
    """ A request class which performs a HEAD request """

    def get_method(self):
        return 'HEAD'

req = HeadRequest(url)
f = urllib2.urlopen(req)
headers = dict(f.headers)
print headers.get('content-length',-1)


--Anand



On Wed, May 7, 2008 at 5:20 PM, Anand Chitipothu <anandology at gmail.com> wrote:
> On Wed, May 7, 2008 at 4:49 PM, Gurpreet Sachdeva
>  <gurpreet.sachdeva at gmail.com> wrote:
>
> > Is there a way in urllib to check file size (from the webserver) before
>  > downloading it? Or any other python module from this?
>
>  If you are on *nix, you can use curl -I to make a HEAD request, which
>  contains Content-Length header.
>
>  $ curl -I http://www.google.com/favicon.ico
>  HTTP/1.1 200 OK
>  Content-Type: image/x-icon
>  Last-Modified: Wed, 07 Jun 2006 19:35:34 GMT
>  Expires: Sun, 17 Jan 2038 19:14:07 GMT
>  Date: Wed, 07 May 2008 11:45:57 GMT
>  Server: gws
>  Content-Length: 1406
>  Age: 7
>
>
> _______________________________________________
>  BangPypers mailing list
>  BangPypers at python.org
>  http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
-Anand


More information about the BangPypers mailing list