How can i find a file size on the web ?

D

Dan Upton

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 ?


When you send an HTTP request, most servers will respond with the
content length. For instance, if you go to http://web-sniffer.net/,
you can type in the file you want to view and you can see the header
sent and received, and for the example you gave, you'll notice the
HTTP Response Header includes a line

Content-Length: 4054802

That should be the information you're looking for.
 
A

Adonis Vargas

Abandoned said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top