Using "Content-Disposition" in HTTP download

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dclist@gmail.com

    #1

    Using "Content-Disposition" in HTTP download

    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).in fo()).

  • Justin Ezequiel

    #2
    Re: Using "Conten t-Disposition&quo t; in HTTP download

    dclist@gmail.co m 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%20stin gray%201%20.JPG '
    >>proxy = 'proxy02:8080'
    >>import httplib
    >>c = httplib.HTTPCon nection(proxy)
    >>c.request('GE T', url)
    >>resp = c.getresponse()
    >>for k in resp.msg.keys() :
    .... print resp.msg.getall matchingheaders (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\x e0\x00\x10JFIF\ x00\x01\x02\x02 \x00\x00\x00\x0 0\x00\x00\xff\x e1\x057Exif\x00 \x00II*\x00\x08 \x00\x00\x00\t\ x00\x0f\x01\x02 \x00\x06\x00\x0 0\x00z\x00\x00\ x00\x10\x01\x02 \x00\x13\x00\x0 0\x00\x80\x00\x 00\x00\x12\x01\ x03\x00\x01\x00 \x00\x00\x01\x0 0\x00\x00\x1a\x 01\x05\x00\x01\ x00\x00\x00\x93 \x00\x00\x00\x1 b\x01\x05\x00\x 01\x00\x00\x00\ x9b\x00\x00\x00 '
    >>resp.fp.close ()
    >>>

    Comment

    • dclist@gmail.com

      #3
      Re: Using "Conten t-Disposition&quo t; in HTTP download


      Justin Ezequiel wrote:
      dclist@gmail.co m 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.

      Comment

      Working...