Using "Content-Disposition" in HTTP download

D

dclist

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()).
 
J

Justin Ezequiel

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?
.... 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()
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top