[BangPypers] How to check file size before downloading it

Anand Chitipothu anandology at gmail.com
Wed May 7 14:21:27 CEST 2008


On Wed, May 7, 2008 at 5:41 PM, Anand Balachandran Pillai
<abpillai at gmail.com> wrote:
> 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)'

Here is another way.

import httplib

conn = httplib.HTTPConnection(host)
conn.request('HEAD', path)
print conn.getresponse().getheader('Content-Type')


More information about the BangPypers mailing list