How can i find a file size on the web ?

Adonis Vargas adonisv at REMOVETHISearthlink.net
Sun Dec 2 12:39:03 EST 2007


Abandoned wrote:
> Hi..
> Can i find a file size witdhout download?
> For example: www.roche.com/rochea_z_sp.pdf
> How can i find its size with python ?
> I'm sorry for my bad english.

Typically you would just do an HTTP HEAD request in order to get 
information about the file without downloading it, but I tested the 
given site and it does not seem to support the HEAD request. Instead I 
used the GET request and read the headers in which contains information 
regarding the file as well, but opted no to read the data to avoid 
downloading it.

import httplib
connection = httplib.HTTPConnection("www.roche.com")
connection.request("GET", "/rochea_z_sp.pdf")
response = connection.getresponse()
print response.status, response.reason
print "File sie: %s" % response.getheader('content-length')

Also you can do:

import urllib
response = urllib.urlopen("http://www.roche.com/rochea_z_sp.pdf")
print response.info()

Hope this helps.

Adonis Vargas



More information about the Python-list mailing list