diff options
author | Guido van Rossum <guido@python.org> | 1996-06-11 00:16:27 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1996-06-11 00:16:27 (GMT) |
commit | a7e4b284223a7cb4d95fe892a3f7224aa3e6e06d (patch) | |
tree | 1010e0a913227f3872cce0e984ad671e2f7755c6 /Lib | |
parent | 34e177780745cce54d2b3f8e69c1fbb2246aaf34 (diff) | |
download | cpython-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')
-rw-r--r-- | Lib/urllib.py | 20 |
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: |