mirror of https://github.com/python/cpython
Change by Sjoerd (with minor reformatting):
guess the mime type of a local file. Change suggested by Sjoerd (with different implementation): when retrieve() creates a temporary file, preserve the suffix. Corrollary of the first change: also return the mime type of a local file in retrieve().
This commit is contained in:
parent
fcc6c68e11
commit
b5916ab065
|
@ -178,15 +178,25 @@ class URLopener:
|
||||||
if not filename and (not type or type == 'file'):
|
if not filename and (not type or type == 'file'):
|
||||||
try:
|
try:
|
||||||
fp = self.open_local_file(url1)
|
fp = self.open_local_file(url1)
|
||||||
|
hdrs = fp.info()
|
||||||
del fp
|
del fp
|
||||||
return url2pathname(splithost(url1)[1]), None
|
return url2pathname(splithost(url1)[1]), hdrs
|
||||||
except IOError, msg:
|
except IOError, msg:
|
||||||
pass
|
pass
|
||||||
fp = self.open(url)
|
fp = self.open(url)
|
||||||
headers = fp.info()
|
headers = fp.info()
|
||||||
if not filename:
|
if not filename:
|
||||||
import tempfile
|
import tempfile
|
||||||
filename = tempfile.mktemp()
|
garbage, path = splittype(url)
|
||||||
|
print (garbage, path)
|
||||||
|
garbage, path = splithost(path or "")
|
||||||
|
print (garbage, path)
|
||||||
|
path, garbage = splitquery(path or "")
|
||||||
|
print (path, garbage)
|
||||||
|
path, garbage = splitattr(path or "")
|
||||||
|
print (path, garbage)
|
||||||
|
suffix = os.path.splitext(path)[1]
|
||||||
|
filename = tempfile.mktemp(suffix)
|
||||||
self.__tempfiles.append(filename)
|
self.__tempfiles.append(filename)
|
||||||
result = filename, headers
|
result = filename, headers
|
||||||
if self.tempcache is not None:
|
if self.tempcache is not None:
|
||||||
|
@ -297,18 +307,22 @@ class URLopener:
|
||||||
|
|
||||||
# Use local file
|
# Use local file
|
||||||
def open_local_file(self, url):
|
def open_local_file(self, url):
|
||||||
|
import mimetypes, mimetools, StringIO
|
||||||
|
mtype = mimetypes.guess_type(url)[0]
|
||||||
|
headers = mimetools.Message(StringIO.StringIO(
|
||||||
|
'Content-Type: %s\n' % (mtype or 'text/plain')))
|
||||||
host, file = splithost(url)
|
host, file = splithost(url)
|
||||||
if not host:
|
if not host:
|
||||||
return addinfourl(
|
return addinfourl(
|
||||||
open(url2pathname(file), 'rb'),
|
open(url2pathname(file), 'rb'),
|
||||||
noheaders(), 'file:'+file)
|
headers, 'file:'+file)
|
||||||
host, port = splitport(host)
|
host, port = splitport(host)
|
||||||
if not port and socket.gethostbyname(host) in (
|
if not port and socket.gethostbyname(host) in (
|
||||||
localhost(), thishost()):
|
localhost(), thishost()):
|
||||||
file = unquote(file)
|
file = unquote(file)
|
||||||
return addinfourl(
|
return addinfourl(
|
||||||
open(url2pathname(file), 'rb'),
|
open(url2pathname(file), 'rb'),
|
||||||
noheaders(), 'file:'+file)
|
headers, 'file:'+file)
|
||||||
raise IOError, ('local file error', 'not on local host')
|
raise IOError, ('local file error', 'not on local host')
|
||||||
|
|
||||||
# Use FTP protocol
|
# Use FTP protocol
|
||||||
|
|
Loading…
Reference in New Issue