Using "Content-Disposition" in HTTP download

Discussion in 'Python' started by dclist@gmail.com, Sep 4, 2006.

  1. Guest

    What is the correct way to download a file through HTTP and save it to
    the file name suggested by "Content-Disposition"?

    I would use urlretrieve but I'm not sure how to obtain the file name
    through the HTTP headers without downloading the body (e.g.
    urlopen(url).info()).
    , Sep 4, 2006
    #1
    1. Advertising

  2. wrote:
    > What is the correct way to download a file through HTTP and save it to
    > the file name suggested by "Content-Disposition"?
    >


    Perhaps something along the lines of the following?

    >>> url = r'http://www.4so9.com/cauca/files/ban-doc/francois/stingray/198%20lb%20stingray%201%20.JPG'
    >>> proxy = 'proxy02:8080'
    >>> import httplib
    >>> c = httplib.HTTPConnection(proxy)
    >>> c.request('GET', url)
    >>> resp = c.getresponse()
    >>> for k in resp.msg.keys():

    .... print resp.msg.getallmatchingheaders(k)
    ....
    ['Content-Length: 64632\r\n']
    ['Proxy-Connection: close\r\n']
    ['X-Cache: HIT from SPSweb\r\n']
    ['Accept-Ranges: bytes\r\n']
    ['Server: Apache/2.0.51 (Fedora)\r\n']
    ['Last-Modified: Fri, 03 Dec 2004 16:57:30 GMT\r\n']
    ['ETag: "3b2375-fc78-8c13280"\r\n']
    ['Date: Tue, 05 Sep 2006 10:35:48 GMT\r\n']
    ['Content-Type: image/jpeg\r\n']
    ['Age: 31440\r\n']
    >>> data = resp.fp.read()
    >>> len(data)

    64632
    >>> data[:100]

    '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x02\x00\x00\x00\x00\x00\x00\xff\xe1\x057Exif\x00\x00II*\x00\x08\x00\x00\x00\t\x00\x0f\x01\x02\x00\x06\x00\x00\x00z\x00\x00\x00\x10\x01\x02\x00\x13\x00\x00\x00\x80\x00\x00\x00\x12\x01\x03\x00\x01\x00\x00\x00\x01\x00\x00\x00\x1a\x01\x05\x00\x01\x00\x00\x00\x93\x00\x00\x00\x1b\x01\x05\x00\x01\x00\x00\x00\x9b\x00\x00\x00'
    >>> resp.fp.close()
    >>>
    Justin Ezequiel, Sep 5, 2006
    #2
    1. Advertising

  3. Guest

    Justin Ezequiel wrote:
    > wrote:
    > > What is the correct way to download a file through HTTP and save it to
    > > the file name suggested by "Content-Disposition"?
    > >

    >
    > Perhaps something along the lines of the following?

    <snip>

    Thank you kindly.
    , Sep 6, 2006
    #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. ben
    Replies:
    0
    Views:
    1,064
  2. =?Utf-8?B?R1JF?=
    Replies:
    1
    Views:
    3,404
    Craig Deelsnyder
    Aug 20, 2004
  3. Replies:
    1
    Views:
    762
  4. Bob Jones
    Replies:
    2
    Views:
    513
    Bob Jones
    Nov 13, 2007
  5. Mike McCarthy
    Replies:
    9
    Views:
    306
    Ray at
    Apr 16, 2004
Loading...

Share This Page