summaryrefslogtreecommitdiffstats
path: root/Lib/urllib.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-06-11 00:16:27 (GMT)
committerGuido van Rossum <guido@python.org>1996-06-11 00:16:27 (GMT)
commita7e4b284223a7cb4d95fe892a3f7224aa3e6e06d (patch)
tree1010e0a913227f3872cce0e984ad671e2f7755c6 /Lib/urllib.py
parent34e177780745cce54d2b3f8e69c1fbb2246aaf34 (diff)
downloadcpython-a7e4b284223a7cb4d95fe892a3f7224aa3e6e06d.zip
cpython-a7e4b284223a7cb4d95fe892a3f7224aa3e6e06d.tar.gz
cpython-a7e4b284223a7cb4d95fe892a3f7224aa3e6e06d.tar.bz2
Support optional filename argument for retrieve() and urlretrieve(),
to specify where it should go (if specified, even local files will be copied into the given file).
Diffstat (limited to 'Lib/urllib.py')
-rw-r--r--Lib/urllib.py20
1 files changed, 12 insertions, 8 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py
index 82a39f2..6caf2b2 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -45,11 +45,14 @@ def urlopen(url):
if not _urlopener:
_urlopener = FancyURLopener()
return _urlopener.open(url)
-def urlretrieve(url):
+def urlretrieve(url, filename=None):
global _urlopener
if not _urlopener:
_urlopener = FancyURLopener()
- return _urlopener.retrieve(url)
+ if filename:
+ return _urlopener.retrieve(url, filename)
+ else:
+ return _urlopener.retrieve(url)
def urlcleanup():
if _urlopener:
_urlopener.cleanup()
@@ -134,7 +137,7 @@ class URLopener:
# External interface
# retrieve(url) returns (filename, None) for a local object
# or (tempfilename, headers) for a remote object
- def retrieve(self, url):
+ def retrieve(self, url, filename=None):
if self.tempcache and self.tempcache.has_key(url):
return self.tempcache[url]
url1 = unwrap(url)
@@ -142,7 +145,7 @@ class URLopener:
self.tempcache[url] = self.tempcache[url1]
return self.tempcache[url1]
type, url1 = splittype(url1)
- if not type or type == 'file':
+ if not filename and (not type or type == 'file'):
try:
fp = self.open_local_file(url1)
del fp
@@ -151,12 +154,13 @@ class URLopener:
pass
fp = self.open(url)
headers = fp.info()
- import tempfile
- tfn = tempfile.mktemp()
- result = tfn, headers
+ if not filename:
+ import tempfile
+ filename = tempfile.mktemp()
+ result = filename, headers
if self.tempcache is not None:
self.tempcache[url] = result
- tfp = open(tfn, 'w')
+ tfp = open(filename, 'w')
bs = 1024*8
block = fp.read(bs)
while block: