I searched for you question on google and I saw that it was answered in stackoverflow before I believe.
Try looking at this post:
Using urllib2 in Python. How do I get the name of the file I am downloading?
The filename is usually included by the server through the content-disposition header:
content-disposition: attachment; filename=foo.pdf
You have access to the headers through
result = urllib2.urlopen(...) result.info() <- contains the headers i>>> import urllib2 ur>>> result = urllib2.urlopen('http://zopyx.com') >>> print result <addinfourl at 4302289808 whose fp = <socket._fileobject object at 0x1006dd5d0>> >>> result.info() <httplib.HTTPMessage instance at 0x1006fbab8> >>> result.info().headers ['Date: Mon, 04 Apr 2011 02:08:28 GMT\r\n', 'Server: Zope/(unreleased version, python 2.4.6, linux2) ZServer/1.1
Plone/3.3.4\r\n', 'Content-Length: 15321\r\n', 'Content-Type: text/html; charset=utf-8\r\n', 'Via: 1.1 www.zopyx.com\r\n', 'Cache-Control: max-age=3600\r\n', 'Expires: Mon, 04 Apr 2011 03:08:28 GMT\r\n', 'Connection: close\r\n']
See
http://docs.python.org/library/urllib2.html