Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python httplib/urllib get filename

is there a possibillity to get the filename

e.g. xyz.com/blafoo/showall.html

if you work with urllib or httplib?

so that i can save the file under the filename on the server?

if you go to sites like

xyz.com/blafoo/ 

you cant see the filename.

Thank you

like image 414
HappyHacking Avatar asked Aug 02 '12 18:08

HappyHacking


3 Answers

Use urllib.request.Request:

import urllib

req = urllib.request.Request(url, method='HEAD')
r = urllib.request.urlopen(req)
print(r.info().get_filename())

Example :

In[1]: urllib.request.urlopen(urllib.request.Request('https://httpbin.org/response-headers?content-disposition=%20attachment%3Bfilename%3D%22example.csv%22', method='HEAD')).info().get_filename()
Out[1]: 'example.csv'
like image 121
the21st Avatar answered Nov 15 '22 17:11

the21st


To get filename from response http headers:

import cgi

response = urllib2.urlopen(URL)
_, params = cgi.parse_header(response.headers.get('Content-Disposition', ''))
filename = params['filename']

To get filename from the URL:

import posixpath
import urlparse 

path = urlparse.urlsplit(URL).path
filename = posixpath.basename(path)
like image 20
jfs Avatar answered Nov 15 '22 17:11

jfs


Does not make much sense what you are asking. The only thing that you have is the URL. Either extract the last part from the URL or you may check the HTTP response for something like

content-disposition: attachment;filename="foo.bar"

This header can be set by the server to indicate that the filename is foo.bar. This is usually used for file downloads or something similar.

like image 38
Andreas Jung Avatar answered Nov 15 '22 17:11

Andreas Jung