How can i find a file size on the web ?

Discussion in 'Python' started by Abandoned, Dec 2, 2007.

  1. Abandoned

    Abandoned Guest

    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.
     
    Abandoned, Dec 2, 2007
    #1
    1. Advertising

  2. Abandoned

    Dan Upton Guest

    On Dec 2, 2007 11:55 AM, 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 ?



    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.
     
    Dan Upton, Dec 2, 2007
    #2
    1. Advertising

  3. 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
     
    Adonis Vargas, Dec 2, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ad
    Replies:
    2
    Views:
    861
  2. tiewknvc9
    Replies:
    6
    Views:
    661
    Chris Uppal
    Oct 1, 2006
  3. Replies:
    2
    Views:
    374
    momobear
    Mar 29, 2007
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,584
    Michael Jung
    May 25, 2008
  5. mark
    Replies:
    34
    Views:
    2,233
    David Thompson
    Nov 21, 2011
Loading...

Share This Page