Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Open an arbitrary URL. |
| 2 | |
| 3 | See the following document for more info on URLs: |
| 4 | "Names and Addresses, URIs, URLs, URNs, URCs", at |
| 5 | http://www.w3.org/pub/WWW/Addressing/Overview.html |
| 6 | |
| 7 | See also the HTTP spec (from which the error codes are derived): |
| 8 | "HTTP - Hypertext Transfer Protocol", at |
| 9 | http://www.w3.org/pub/WWW/Protocols/ |
| 10 | |
| 11 | Related standards and specs: |
| 12 | - RFC1808: the "relative URL" spec. (authoritative status) |
| 13 | - RFC1738 - the "URL standard". (authoritative status) |
| 14 | - RFC1630 - the "URI spec". (informational status) |
| 15 | |
| 16 | The object returned by URLopener().open(file) will differ per |
| 17 | protocol. All you know is that is has methods read(), readline(), |
| 18 | readlines(), fileno(), close() and info(). The read*(), fileno() |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 19 | and close() methods work like those of open files. |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 20 | The info() method returns a mimetools.Message object which can be |
| 21 | used to query various info about the object, if available. |
| 22 | (mimetools.Message objects are queried with the getheader() method.) |
| 23 | """ |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 25 | import string |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 26 | import socket |
Jack Jansen | dc3e3f6 | 1995-12-15 13:22:13 +0000 | [diff] [blame] | 27 | import os |
Guido van Rossum | f0713d3 | 2001-08-09 17:43:35 +0000 | [diff] [blame] | 28 | import time |
Guido van Rossum | 3c8484e | 1996-11-20 22:02:24 +0000 | [diff] [blame] | 29 | import sys |
Brett Cannon | 69200fa | 2004-03-23 21:26:39 +0000 | [diff] [blame] | 30 | from urlparse import urljoin as basejoin |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 31 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 32 | __all__ = ["urlopen", "URLopener", "FancyURLopener", "urlretrieve", |
| 33 | "urlcleanup", "quote", "quote_plus", "unquote", "unquote_plus", |
Skip Montanaro | 44d5e0c | 2001-03-13 19:47:16 +0000 | [diff] [blame] | 34 | "urlencode", "url2pathname", "pathname2url", "splittag", |
| 35 | "localhost", "thishost", "ftperrors", "basejoin", "unwrap", |
| 36 | "splittype", "splithost", "splituser", "splitpasswd", "splitport", |
| 37 | "splitnport", "splitquery", "splitattr", "splitvalue", |
| 38 | "splitgophertype", "getproxies"] |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 39 | |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 40 | __version__ = '1.17' # XXX This version is not always updated :-( |
Guido van Rossum | f668d17 | 1997-06-06 21:11:11 +0000 | [diff] [blame] | 41 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 42 | MAXFTPCACHE = 10 # Trim the ftp cache beyond this size |
Guido van Rossum | 6cb15a0 | 1995-06-22 19:00:13 +0000 | [diff] [blame] | 43 | |
Jack Jansen | dc3e3f6 | 1995-12-15 13:22:13 +0000 | [diff] [blame] | 44 | # Helper for non-unix systems |
| 45 | if os.name == 'mac': |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 46 | from macurl2path import url2pathname, pathname2url |
Guido van Rossum | 7e7ca0b | 1998-03-26 21:01:39 +0000 | [diff] [blame] | 47 | elif os.name == 'nt': |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 48 | from nturl2path import url2pathname, pathname2url |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 49 | elif os.name == 'riscos': |
| 50 | from rourl2path import url2pathname, pathname2url |
Jack Jansen | dc3e3f6 | 1995-12-15 13:22:13 +0000 | [diff] [blame] | 51 | else: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 52 | def url2pathname(pathname): |
Georg Brandl | c0b2473 | 2005-12-26 22:53:56 +0000 | [diff] [blame] | 53 | """OS-specific conversion from a relative URL of the 'file' scheme |
| 54 | to a file system path; not recommended for general use.""" |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 55 | return unquote(pathname) |
Georg Brandl | c0b2473 | 2005-12-26 22:53:56 +0000 | [diff] [blame] | 56 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 57 | def pathname2url(pathname): |
Georg Brandl | c0b2473 | 2005-12-26 22:53:56 +0000 | [diff] [blame] | 58 | """OS-specific conversion from a file system path to a relative URL |
| 59 | of the 'file' scheme; not recommended for general use.""" |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 60 | return quote(pathname) |
Guido van Rossum | 33add0a | 1998-12-18 15:25:22 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 62 | # This really consists of two pieces: |
| 63 | # (1) a class which handles opening of all sorts of URLs |
| 64 | # (plus assorted utilities etc.) |
| 65 | # (2) a set of functions for parsing URLs |
| 66 | # XXX Should these be separated out into different modules? |
| 67 | |
| 68 | |
| 69 | # Shortcut for basic usage |
| 70 | _urlopener = None |
Fred Drake | df6eca7 | 2002-04-04 20:41:34 +0000 | [diff] [blame] | 71 | def urlopen(url, data=None, proxies=None): |
Skip Montanaro | 79f1c17 | 2000-08-22 03:00:52 +0000 | [diff] [blame] | 72 | """urlopen(url [, data]) -> open file-like object""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 73 | global _urlopener |
Fred Drake | df6eca7 | 2002-04-04 20:41:34 +0000 | [diff] [blame] | 74 | if proxies is not None: |
| 75 | opener = FancyURLopener(proxies=proxies) |
| 76 | elif not _urlopener: |
| 77 | opener = FancyURLopener() |
| 78 | _urlopener = opener |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 79 | else: |
Fred Drake | df6eca7 | 2002-04-04 20:41:34 +0000 | [diff] [blame] | 80 | opener = _urlopener |
| 81 | if data is None: |
| 82 | return opener.open(url) |
| 83 | else: |
| 84 | return opener.open(url, data) |
Fred Drake | 316a793 | 2000-08-24 01:01:26 +0000 | [diff] [blame] | 85 | def urlretrieve(url, filename=None, reporthook=None, data=None): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 86 | global _urlopener |
| 87 | if not _urlopener: |
| 88 | _urlopener = FancyURLopener() |
Fred Drake | 316a793 | 2000-08-24 01:01:26 +0000 | [diff] [blame] | 89 | return _urlopener.retrieve(url, filename, reporthook, data) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 90 | def urlcleanup(): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 91 | if _urlopener: |
| 92 | _urlopener.cleanup() |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 93 | |
Georg Brandl | b925602 | 2005-08-24 18:46:39 +0000 | [diff] [blame] | 94 | # exception raised when downloaded size does not match content-length |
| 95 | class ContentTooShortError(IOError): |
| 96 | def __init__(self, message, content): |
| 97 | IOError.__init__(self, message) |
| 98 | self.content = content |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 100 | ftpcache = {} |
| 101 | class URLopener: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 102 | """Class to open URLs. |
| 103 | This is a class rather than just a subroutine because we may need |
| 104 | more than one set of global protocol-specific options. |
| 105 | Note -- this is a base class for those who don't want the |
| 106 | automatic handling of errors type 302 (relocated) and 401 |
| 107 | (authorization needed).""" |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 108 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 109 | __tempfiles = None |
Guido van Rossum | 29e7781 | 1996-11-27 19:39:58 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | ba31138 | 2000-08-24 16:18:04 +0000 | [diff] [blame] | 111 | version = "Python-urllib/%s" % __version__ |
| 112 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 113 | # Constructor |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 114 | def __init__(self, proxies=None, **x509): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 115 | if proxies is None: |
| 116 | proxies = getproxies() |
| 117 | assert hasattr(proxies, 'has_key'), "proxies must be a mapping" |
| 118 | self.proxies = proxies |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 119 | self.key_file = x509.get('key_file') |
| 120 | self.cert_file = x509.get('cert_file') |
Guido van Rossum | ba31138 | 2000-08-24 16:18:04 +0000 | [diff] [blame] | 121 | self.addheaders = [('User-agent', self.version)] |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 122 | self.__tempfiles = [] |
| 123 | self.__unlink = os.unlink # See cleanup() |
| 124 | self.tempcache = None |
| 125 | # Undocumented feature: if you assign {} to tempcache, |
| 126 | # it is used to cache files retrieved with |
| 127 | # self.retrieve(). This is not enabled by default |
| 128 | # since it does not work for changing documents (and I |
| 129 | # haven't got the logic to check expiration headers |
| 130 | # yet). |
| 131 | self.ftpcache = ftpcache |
| 132 | # Undocumented feature: you can use a different |
| 133 | # ftp cache by assigning to the .ftpcache member; |
| 134 | # in case you want logically independent URL openers |
| 135 | # XXX This is not threadsafe. Bah. |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 136 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 137 | def __del__(self): |
| 138 | self.close() |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 139 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 140 | def close(self): |
| 141 | self.cleanup() |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 142 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 143 | def cleanup(self): |
| 144 | # This code sometimes runs when the rest of this module |
| 145 | # has already been deleted, so it can't use any globals |
| 146 | # or import anything. |
| 147 | if self.__tempfiles: |
| 148 | for file in self.__tempfiles: |
| 149 | try: |
| 150 | self.__unlink(file) |
Martin v. Löwis | 58682b7 | 2001-08-11 15:02:57 +0000 | [diff] [blame] | 151 | except OSError: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 152 | pass |
| 153 | del self.__tempfiles[:] |
| 154 | if self.tempcache: |
| 155 | self.tempcache.clear() |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 156 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 157 | def addheader(self, *args): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 158 | """Add a header to be used by the HTTP interface only |
| 159 | e.g. u.addheader('Accept', 'sound/basic')""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 160 | self.addheaders.append(args) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 161 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 162 | # External interface |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 163 | def open(self, fullurl, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 164 | """Use URLopener().open(file) instead of open(file, 'r').""" |
Martin v. Löwis | 1d99433 | 2000-12-03 18:30:10 +0000 | [diff] [blame] | 165 | fullurl = unwrap(toBytes(fullurl)) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 166 | if self.tempcache and fullurl in self.tempcache: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 167 | filename, headers = self.tempcache[fullurl] |
| 168 | fp = open(filename, 'rb') |
| 169 | return addinfourl(fp, headers, fullurl) |
Martin v. Löwis | 1d99433 | 2000-12-03 18:30:10 +0000 | [diff] [blame] | 170 | urltype, url = splittype(fullurl) |
| 171 | if not urltype: |
| 172 | urltype = 'file' |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 173 | if urltype in self.proxies: |
Martin v. Löwis | 1d99433 | 2000-12-03 18:30:10 +0000 | [diff] [blame] | 174 | proxy = self.proxies[urltype] |
| 175 | urltype, proxyhost = splittype(proxy) |
Jeremy Hylton | d52755f | 2000-10-02 23:04:02 +0000 | [diff] [blame] | 176 | host, selector = splithost(proxyhost) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 177 | url = (host, fullurl) # Signal special case to open_*() |
Jeremy Hylton | d52755f | 2000-10-02 23:04:02 +0000 | [diff] [blame] | 178 | else: |
| 179 | proxy = None |
Martin v. Löwis | 1d99433 | 2000-12-03 18:30:10 +0000 | [diff] [blame] | 180 | name = 'open_' + urltype |
| 181 | self.type = urltype |
Brett Cannon | aaeffaf | 2004-03-23 23:50:17 +0000 | [diff] [blame] | 182 | name = name.replace('-', '_') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 183 | if not hasattr(self, name): |
Jeremy Hylton | d52755f | 2000-10-02 23:04:02 +0000 | [diff] [blame] | 184 | if proxy: |
| 185 | return self.open_unknown_proxy(proxy, fullurl, data) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 186 | else: |
| 187 | return self.open_unknown(fullurl, data) |
| 188 | try: |
| 189 | if data is None: |
| 190 | return getattr(self, name)(url) |
| 191 | else: |
| 192 | return getattr(self, name)(url, data) |
| 193 | except socket.error, msg: |
| 194 | raise IOError, ('socket error', msg), sys.exc_info()[2] |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 195 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 196 | def open_unknown(self, fullurl, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 197 | """Overridable interface to open unknown URL type.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 198 | type, url = splittype(fullurl) |
| 199 | raise IOError, ('url error', 'unknown url type', type) |
Guido van Rossum | ca44540 | 1995-08-29 19:19:12 +0000 | [diff] [blame] | 200 | |
Jeremy Hylton | d52755f | 2000-10-02 23:04:02 +0000 | [diff] [blame] | 201 | def open_unknown_proxy(self, proxy, fullurl, data=None): |
| 202 | """Overridable interface to open unknown URL type.""" |
| 203 | type, url = splittype(fullurl) |
| 204 | raise IOError, ('url error', 'invalid proxy for %s' % type, proxy) |
| 205 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 206 | # External interface |
Sjoerd Mullender | d7b86f0 | 2000-08-25 11:23:36 +0000 | [diff] [blame] | 207 | def retrieve(self, url, filename=None, reporthook=None, data=None): |
Brett Cannon | 7d618c7 | 2003-04-24 02:43:20 +0000 | [diff] [blame] | 208 | """retrieve(url) returns (filename, headers) for a local object |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 209 | or (tempfilename, headers) for a remote object.""" |
Martin v. Löwis | 1d99433 | 2000-12-03 18:30:10 +0000 | [diff] [blame] | 210 | url = unwrap(toBytes(url)) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 211 | if self.tempcache and url in self.tempcache: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 212 | return self.tempcache[url] |
| 213 | type, url1 = splittype(url) |
Raymond Hettinger | 10ff706 | 2002-06-02 03:04:52 +0000 | [diff] [blame] | 214 | if filename is None and (not type or type == 'file'): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 215 | try: |
| 216 | fp = self.open_local_file(url1) |
| 217 | hdrs = fp.info() |
| 218 | del fp |
| 219 | return url2pathname(splithost(url1)[1]), hdrs |
| 220 | except IOError, msg: |
| 221 | pass |
Fred Drake | 316a793 | 2000-08-24 01:01:26 +0000 | [diff] [blame] | 222 | fp = self.open(url, data) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 223 | headers = fp.info() |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 224 | if filename: |
| 225 | tfp = open(filename, 'wb') |
| 226 | else: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 227 | import tempfile |
| 228 | garbage, path = splittype(url) |
| 229 | garbage, path = splithost(path or "") |
| 230 | path, garbage = splitquery(path or "") |
| 231 | path, garbage = splitattr(path or "") |
| 232 | suffix = os.path.splitext(path)[1] |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 233 | (fd, filename) = tempfile.mkstemp(suffix) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 234 | self.__tempfiles.append(filename) |
Jeremy Hylton | 3bd6fde | 2002-10-11 14:36:24 +0000 | [diff] [blame] | 235 | tfp = os.fdopen(fd, 'wb') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 236 | result = filename, headers |
| 237 | if self.tempcache is not None: |
| 238 | self.tempcache[url] = result |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 239 | bs = 1024*8 |
| 240 | size = -1 |
Georg Brandl | b925602 | 2005-08-24 18:46:39 +0000 | [diff] [blame] | 241 | read = 0 |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 242 | blocknum = 0 |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 243 | if reporthook: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 244 | if "content-length" in headers: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 245 | size = int(headers["Content-Length"]) |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 246 | reporthook(blocknum, bs, size) |
| 247 | while 1: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 248 | block = fp.read(bs) |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 249 | if block == "": |
| 250 | break |
Georg Brandl | b925602 | 2005-08-24 18:46:39 +0000 | [diff] [blame] | 251 | read += len(block) |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 252 | tfp.write(block) |
Georg Brandl | b925602 | 2005-08-24 18:46:39 +0000 | [diff] [blame] | 253 | blocknum += 1 |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 254 | if reporthook: |
| 255 | reporthook(blocknum, bs, size) |
| 256 | fp.close() |
| 257 | tfp.close() |
| 258 | del fp |
| 259 | del tfp |
Georg Brandl | b925602 | 2005-08-24 18:46:39 +0000 | [diff] [blame] | 260 | |
| 261 | # raise exception if actual size does not match content-length header |
| 262 | if size >= 0 and read < size: |
| 263 | raise ContentTooShortError("retrieval incomplete: got only %i out " |
| 264 | "of %i bytes" % (read, size), result) |
| 265 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 266 | return result |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 267 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 268 | # Each method named open_<type> knows how to open that type of URL |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 269 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 270 | def open_http(self, url, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 271 | """Use HTTP protocol.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 272 | import httplib |
| 273 | user_passwd = None |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 274 | proxy_passwd= None |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 275 | if isinstance(url, str): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 276 | host, selector = splithost(url) |
| 277 | if host: |
| 278 | user_passwd, host = splituser(host) |
| 279 | host = unquote(host) |
| 280 | realhost = host |
| 281 | else: |
| 282 | host, selector = url |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 283 | # check whether the proxy contains authorization information |
| 284 | proxy_passwd, host = splituser(host) |
| 285 | # now we proceed with the url we want to obtain |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 286 | urltype, rest = splittype(selector) |
| 287 | url = rest |
| 288 | user_passwd = None |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 289 | if urltype.lower() != 'http': |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 290 | realhost = None |
| 291 | else: |
| 292 | realhost, rest = splithost(rest) |
| 293 | if realhost: |
| 294 | user_passwd, realhost = splituser(realhost) |
| 295 | if user_passwd: |
| 296 | selector = "%s://%s%s" % (urltype, realhost, rest) |
Tim Peters | 55c12d4 | 2001-08-09 18:04:14 +0000 | [diff] [blame] | 297 | if proxy_bypass(realhost): |
| 298 | host = realhost |
| 299 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 300 | #print "proxy via http:", host, selector |
| 301 | if not host: raise IOError, ('http error', 'no host given') |
Tim Peters | 92037a1 | 2006-01-24 22:44:08 +0000 | [diff] [blame] | 302 | |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 303 | if proxy_passwd: |
| 304 | import base64 |
| 305 | proxy_auth = base64.encodestring(proxy_passwd).strip() |
| 306 | else: |
| 307 | proxy_auth = None |
| 308 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 309 | if user_passwd: |
| 310 | import base64 |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 311 | auth = base64.encodestring(user_passwd).strip() |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 312 | else: |
| 313 | auth = None |
| 314 | h = httplib.HTTP(host) |
| 315 | if data is not None: |
| 316 | h.putrequest('POST', selector) |
| 317 | h.putheader('Content-type', 'application/x-www-form-urlencoded') |
| 318 | h.putheader('Content-length', '%d' % len(data)) |
| 319 | else: |
| 320 | h.putrequest('GET', selector) |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 321 | if proxy_auth: h.putheader('Proxy-Authorization', 'Basic %s' % proxy_auth) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 322 | if auth: h.putheader('Authorization', 'Basic %s' % auth) |
| 323 | if realhost: h.putheader('Host', realhost) |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 324 | for args in self.addheaders: h.putheader(*args) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 325 | h.endheaders() |
| 326 | if data is not None: |
Fred Drake | ec3dfde | 2001-07-04 05:18:29 +0000 | [diff] [blame] | 327 | h.send(data) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 328 | errcode, errmsg, headers = h.getreply() |
| 329 | fp = h.getfile() |
| 330 | if errcode == 200: |
| 331 | return addinfourl(fp, headers, "http:" + url) |
| 332 | else: |
| 333 | if data is None: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 334 | return self.http_error(url, fp, errcode, errmsg, headers) |
Guido van Rossum | 29aab75 | 1999-03-09 19:31:21 +0000 | [diff] [blame] | 335 | else: |
| 336 | return self.http_error(url, fp, errcode, errmsg, headers, data) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 337 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 338 | def http_error(self, url, fp, errcode, errmsg, headers, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 339 | """Handle http errors. |
| 340 | Derived class can override this, or provide specific handlers |
| 341 | named http_error_DDD where DDD is the 3-digit error code.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 342 | # First check if there's a specific handler for this error |
| 343 | name = 'http_error_%d' % errcode |
| 344 | if hasattr(self, name): |
| 345 | method = getattr(self, name) |
| 346 | if data is None: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 347 | result = method(url, fp, errcode, errmsg, headers) |
Jeremy Hylton | b30f52a | 1999-02-25 16:14:58 +0000 | [diff] [blame] | 348 | else: |
| 349 | result = method(url, fp, errcode, errmsg, headers, data) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 350 | if result: return result |
Jeremy Hylton | b30f52a | 1999-02-25 16:14:58 +0000 | [diff] [blame] | 351 | return self.http_error_default(url, fp, errcode, errmsg, headers) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 352 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 353 | def http_error_default(self, url, fp, errcode, errmsg, headers): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 354 | """Default error handler: close the connection and raise IOError.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 355 | void = fp.read() |
| 356 | fp.close() |
| 357 | raise IOError, ('http error', errcode, errmsg, headers) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 358 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 359 | if hasattr(socket, "ssl"): |
Andrew M. Kuchling | 141e989 | 2000-04-23 02:53:11 +0000 | [diff] [blame] | 360 | def open_https(self, url, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 361 | """Use HTTPS protocol.""" |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 362 | import httplib |
Fred Drake | 567ca8e | 2000-08-21 21:42:42 +0000 | [diff] [blame] | 363 | user_passwd = None |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 364 | proxy_passwd = None |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 365 | if isinstance(url, str): |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 366 | host, selector = splithost(url) |
Fred Drake | 567ca8e | 2000-08-21 21:42:42 +0000 | [diff] [blame] | 367 | if host: |
| 368 | user_passwd, host = splituser(host) |
| 369 | host = unquote(host) |
| 370 | realhost = host |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 371 | else: |
| 372 | host, selector = url |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 373 | # here, we determine, whether the proxy contains authorization information |
| 374 | proxy_passwd, host = splituser(host) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 375 | urltype, rest = splittype(selector) |
Fred Drake | 567ca8e | 2000-08-21 21:42:42 +0000 | [diff] [blame] | 376 | url = rest |
| 377 | user_passwd = None |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 378 | if urltype.lower() != 'https': |
Fred Drake | 567ca8e | 2000-08-21 21:42:42 +0000 | [diff] [blame] | 379 | realhost = None |
| 380 | else: |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 381 | realhost, rest = splithost(rest) |
Fred Drake | 567ca8e | 2000-08-21 21:42:42 +0000 | [diff] [blame] | 382 | if realhost: |
| 383 | user_passwd, realhost = splituser(realhost) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 384 | if user_passwd: |
| 385 | selector = "%s://%s%s" % (urltype, realhost, rest) |
Andrew M. Kuchling | 7ad4792 | 2000-06-10 01:41:48 +0000 | [diff] [blame] | 386 | #print "proxy via https:", host, selector |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 387 | if not host: raise IOError, ('https error', 'no host given') |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 388 | if proxy_passwd: |
| 389 | import base64 |
| 390 | proxy_auth = base64.encodestring(proxy_passwd).strip() |
| 391 | else: |
| 392 | proxy_auth = None |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 393 | if user_passwd: |
| 394 | import base64 |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 395 | auth = base64.encodestring(user_passwd).strip() |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 396 | else: |
| 397 | auth = None |
| 398 | h = httplib.HTTPS(host, 0, |
| 399 | key_file=self.key_file, |
| 400 | cert_file=self.cert_file) |
Andrew M. Kuchling | 141e989 | 2000-04-23 02:53:11 +0000 | [diff] [blame] | 401 | if data is not None: |
| 402 | h.putrequest('POST', selector) |
| 403 | h.putheader('Content-type', |
| 404 | 'application/x-www-form-urlencoded') |
| 405 | h.putheader('Content-length', '%d' % len(data)) |
| 406 | else: |
| 407 | h.putrequest('GET', selector) |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 408 | if proxy_auth: h.putheader('Proxy-Authorization: Basic %s' % proxy_auth) |
| 409 | if auth: h.putheader('Authorization: Basic %s' % auth) |
Fred Drake | 567ca8e | 2000-08-21 21:42:42 +0000 | [diff] [blame] | 410 | if realhost: h.putheader('Host', realhost) |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 411 | for args in self.addheaders: h.putheader(*args) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 412 | h.endheaders() |
Andrew M. Kuchling | 43c5af0 | 2000-04-24 14:17:06 +0000 | [diff] [blame] | 413 | if data is not None: |
Fred Drake | ec3dfde | 2001-07-04 05:18:29 +0000 | [diff] [blame] | 414 | h.send(data) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 415 | errcode, errmsg, headers = h.getreply() |
| 416 | fp = h.getfile() |
| 417 | if errcode == 200: |
Guido van Rossum | b931bf3 | 2001-12-08 17:09:07 +0000 | [diff] [blame] | 418 | return addinfourl(fp, headers, "https:" + url) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 419 | else: |
Fred Drake | 567ca8e | 2000-08-21 21:42:42 +0000 | [diff] [blame] | 420 | if data is None: |
| 421 | return self.http_error(url, fp, errcode, errmsg, headers) |
| 422 | else: |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 423 | return self.http_error(url, fp, errcode, errmsg, headers, |
| 424 | data) |
Fred Drake | 567ca8e | 2000-08-21 21:42:42 +0000 | [diff] [blame] | 425 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 426 | def open_gopher(self, url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 427 | """Use Gopher protocol.""" |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 428 | if not isinstance(url, str): |
| 429 | raise IOError, ('gopher error', 'proxy support for gopher protocol currently not implemented') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 430 | import gopherlib |
| 431 | host, selector = splithost(url) |
| 432 | if not host: raise IOError, ('gopher error', 'no host given') |
| 433 | host = unquote(host) |
| 434 | type, selector = splitgophertype(selector) |
| 435 | selector, query = splitquery(selector) |
| 436 | selector = unquote(selector) |
| 437 | if query: |
| 438 | query = unquote(query) |
| 439 | fp = gopherlib.send_query(selector, query, host) |
| 440 | else: |
| 441 | fp = gopherlib.send_selector(selector, host) |
| 442 | return addinfourl(fp, noheaders(), "gopher:" + url) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 443 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 444 | def open_file(self, url): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 445 | """Use local file or FTP depending on form of URL.""" |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 446 | if not isinstance(url, str): |
| 447 | raise IOError, ('file error', 'proxy support for file protocol currently not implemented') |
Jack Jansen | 4ef1103 | 2002-09-12 20:14:04 +0000 | [diff] [blame] | 448 | if url[:2] == '//' and url[2:3] != '/' and url[2:12].lower() != 'localhost/': |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 449 | return self.open_ftp(url) |
| 450 | else: |
| 451 | return self.open_local_file(url) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 452 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 453 | def open_local_file(self, url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 454 | """Use local file.""" |
Raymond Hettinger | a617271 | 2004-12-31 19:15:26 +0000 | [diff] [blame] | 455 | import mimetypes, mimetools, email.Utils |
| 456 | try: |
| 457 | from cStringIO import StringIO |
| 458 | except ImportError: |
| 459 | from StringIO import StringIO |
Guido van Rossum | f0713d3 | 2001-08-09 17:43:35 +0000 | [diff] [blame] | 460 | host, file = splithost(url) |
| 461 | localname = url2pathname(file) |
Guido van Rossum | a2da305 | 2002-04-15 00:25:01 +0000 | [diff] [blame] | 462 | try: |
| 463 | stats = os.stat(localname) |
| 464 | except OSError, e: |
| 465 | raise IOError(e.errno, e.strerror, e.filename) |
Walter Dörwald | 92b48b7 | 2002-03-22 17:30:38 +0000 | [diff] [blame] | 466 | size = stats.st_size |
Anthony Baxter | 3dd9e46 | 2004-10-11 13:53:08 +0000 | [diff] [blame] | 467 | modified = email.Utils.formatdate(stats.st_mtime, usegmt=True) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 468 | mtype = mimetypes.guess_type(url)[0] |
Raymond Hettinger | a617271 | 2004-12-31 19:15:26 +0000 | [diff] [blame] | 469 | headers = mimetools.Message(StringIO( |
Guido van Rossum | f0713d3 | 2001-08-09 17:43:35 +0000 | [diff] [blame] | 470 | 'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' % |
| 471 | (mtype or 'text/plain', size, modified))) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 472 | if not host: |
Guido van Rossum | 336a201 | 1999-06-24 15:27:36 +0000 | [diff] [blame] | 473 | urlfile = file |
| 474 | if file[:1] == '/': |
| 475 | urlfile = 'file://' + file |
Guido van Rossum | f0713d3 | 2001-08-09 17:43:35 +0000 | [diff] [blame] | 476 | return addinfourl(open(localname, 'rb'), |
Guido van Rossum | 336a201 | 1999-06-24 15:27:36 +0000 | [diff] [blame] | 477 | headers, urlfile) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 478 | host, port = splitport(host) |
| 479 | if not port \ |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 480 | and socket.gethostbyname(host) in (localhost(), thishost()): |
Guido van Rossum | 336a201 | 1999-06-24 15:27:36 +0000 | [diff] [blame] | 481 | urlfile = file |
| 482 | if file[:1] == '/': |
| 483 | urlfile = 'file://' + file |
Guido van Rossum | f0713d3 | 2001-08-09 17:43:35 +0000 | [diff] [blame] | 484 | return addinfourl(open(localname, 'rb'), |
Guido van Rossum | 336a201 | 1999-06-24 15:27:36 +0000 | [diff] [blame] | 485 | headers, urlfile) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 486 | raise IOError, ('local file error', 'not on local host') |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 487 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 488 | def open_ftp(self, url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 489 | """Use FTP protocol.""" |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 490 | if not isinstance(url, str): |
| 491 | raise IOError, ('ftp error', 'proxy support for ftp protocol currently not implemented') |
Raymond Hettinger | a617271 | 2004-12-31 19:15:26 +0000 | [diff] [blame] | 492 | import mimetypes, mimetools |
| 493 | try: |
| 494 | from cStringIO import StringIO |
| 495 | except ImportError: |
| 496 | from StringIO import StringIO |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 497 | host, path = splithost(url) |
| 498 | if not host: raise IOError, ('ftp error', 'no host given') |
| 499 | host, port = splitport(host) |
| 500 | user, host = splituser(host) |
| 501 | if user: user, passwd = splitpasswd(user) |
| 502 | else: passwd = None |
| 503 | host = unquote(host) |
| 504 | user = unquote(user or '') |
| 505 | passwd = unquote(passwd or '') |
| 506 | host = socket.gethostbyname(host) |
| 507 | if not port: |
| 508 | import ftplib |
| 509 | port = ftplib.FTP_PORT |
| 510 | else: |
| 511 | port = int(port) |
| 512 | path, attrs = splitattr(path) |
| 513 | path = unquote(path) |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 514 | dirs = path.split('/') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 515 | dirs, file = dirs[:-1], dirs[-1] |
| 516 | if dirs and not dirs[0]: dirs = dirs[1:] |
Guido van Rossum | 5e006a3 | 1999-08-18 17:40:33 +0000 | [diff] [blame] | 517 | if dirs and not dirs[0]: dirs[0] = '/' |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 518 | key = user, host, port, '/'.join(dirs) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 519 | # XXX thread unsafe! |
| 520 | if len(self.ftpcache) > MAXFTPCACHE: |
| 521 | # Prune the cache, rather arbitrarily |
| 522 | for k in self.ftpcache.keys(): |
| 523 | if k != key: |
| 524 | v = self.ftpcache[k] |
| 525 | del self.ftpcache[k] |
| 526 | v.close() |
| 527 | try: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 528 | if not key in self.ftpcache: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 529 | self.ftpcache[key] = \ |
| 530 | ftpwrapper(user, passwd, host, port, dirs) |
| 531 | if not file: type = 'D' |
| 532 | else: type = 'I' |
| 533 | for attr in attrs: |
| 534 | attr, value = splitvalue(attr) |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 535 | if attr.lower() == 'type' and \ |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 536 | value in ('a', 'A', 'i', 'I', 'd', 'D'): |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 537 | type = value.upper() |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 538 | (fp, retrlen) = self.ftpcache[key].retrfile(file, type) |
Guido van Rossum | 88e0b5b | 2001-08-23 13:38:15 +0000 | [diff] [blame] | 539 | mtype = mimetypes.guess_type("ftp:" + url)[0] |
| 540 | headers = "" |
| 541 | if mtype: |
| 542 | headers += "Content-Type: %s\n" % mtype |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 543 | if retrlen is not None and retrlen >= 0: |
Guido van Rossum | 88e0b5b | 2001-08-23 13:38:15 +0000 | [diff] [blame] | 544 | headers += "Content-Length: %d\n" % retrlen |
Raymond Hettinger | a617271 | 2004-12-31 19:15:26 +0000 | [diff] [blame] | 545 | headers = mimetools.Message(StringIO(headers)) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 546 | return addinfourl(fp, headers, "ftp:" + url) |
| 547 | except ftperrors(), msg: |
| 548 | raise IOError, ('ftp error', msg), sys.exc_info()[2] |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 549 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 550 | def open_data(self, url, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 551 | """Use "data" URL.""" |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 552 | if not isinstance(url, str): |
| 553 | raise IOError, ('data error', 'proxy support for data protocol currently not implemented') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 554 | # ignore POSTed data |
| 555 | # |
| 556 | # syntax of data URLs: |
| 557 | # dataurl := "data:" [ mediatype ] [ ";base64" ] "," data |
| 558 | # mediatype := [ type "/" subtype ] *( ";" parameter ) |
| 559 | # data := *urlchar |
| 560 | # parameter := attribute "=" value |
Raymond Hettinger | a617271 | 2004-12-31 19:15:26 +0000 | [diff] [blame] | 561 | import mimetools |
| 562 | try: |
| 563 | from cStringIO import StringIO |
| 564 | except ImportError: |
| 565 | from StringIO import StringIO |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 566 | try: |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 567 | [type, data] = url.split(',', 1) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 568 | except ValueError: |
| 569 | raise IOError, ('data error', 'bad data URL') |
| 570 | if not type: |
| 571 | type = 'text/plain;charset=US-ASCII' |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 572 | semi = type.rfind(';') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 573 | if semi >= 0 and '=' not in type[semi:]: |
| 574 | encoding = type[semi+1:] |
| 575 | type = type[:semi] |
| 576 | else: |
| 577 | encoding = '' |
| 578 | msg = [] |
| 579 | msg.append('Date: %s'%time.strftime('%a, %d %b %Y %T GMT', |
| 580 | time.gmtime(time.time()))) |
| 581 | msg.append('Content-type: %s' % type) |
| 582 | if encoding == 'base64': |
| 583 | import base64 |
| 584 | data = base64.decodestring(data) |
| 585 | else: |
| 586 | data = unquote(data) |
| 587 | msg.append('Content-length: %d' % len(data)) |
| 588 | msg.append('') |
| 589 | msg.append(data) |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 590 | msg = '\n'.join(msg) |
Raymond Hettinger | a617271 | 2004-12-31 19:15:26 +0000 | [diff] [blame] | 591 | f = StringIO(msg) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 592 | headers = mimetools.Message(f, 0) |
Georg Brandl | 1f66357 | 2005-11-26 16:50:44 +0000 | [diff] [blame] | 593 | #f.fileno = None # needed for addinfourl |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 594 | return addinfourl(f, headers, url) |
Guido van Rossum | 6d4d1c2 | 1998-03-12 14:32:55 +0000 | [diff] [blame] | 595 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 597 | class FancyURLopener(URLopener): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 598 | """Derived class with handlers for errors we can handle (perhaps).""" |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 599 | |
Neal Norwitz | 60e04cd | 2002-06-11 13:38:51 +0000 | [diff] [blame] | 600 | def __init__(self, *args, **kwargs): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 601 | URLopener.__init__(self, *args, **kwargs) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 602 | self.auth_cache = {} |
Skip Montanaro | c3e11d6 | 2001-02-15 16:56:36 +0000 | [diff] [blame] | 603 | self.tries = 0 |
| 604 | self.maxtries = 10 |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 605 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 606 | def http_error_default(self, url, fp, errcode, errmsg, headers): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 607 | """Default error handling -- don't raise an exception.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 608 | return addinfourl(fp, headers, "http:" + url) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 609 | |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 610 | def http_error_302(self, url, fp, errcode, errmsg, headers, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 611 | """Error 302 -- relocated (temporarily).""" |
Skip Montanaro | c3e11d6 | 2001-02-15 16:56:36 +0000 | [diff] [blame] | 612 | self.tries += 1 |
| 613 | if self.maxtries and self.tries >= self.maxtries: |
| 614 | if hasattr(self, "http_error_500"): |
| 615 | meth = self.http_error_500 |
| 616 | else: |
| 617 | meth = self.http_error_default |
| 618 | self.tries = 0 |
| 619 | return meth(url, fp, 500, |
| 620 | "Internal Server Error: Redirect Recursion", headers) |
| 621 | result = self.redirect_internal(url, fp, errcode, errmsg, headers, |
| 622 | data) |
| 623 | self.tries = 0 |
| 624 | return result |
| 625 | |
| 626 | def redirect_internal(self, url, fp, errcode, errmsg, headers, data): |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 627 | if 'location' in headers: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 628 | newurl = headers['location'] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 629 | elif 'uri' in headers: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 630 | newurl = headers['uri'] |
| 631 | else: |
| 632 | return |
| 633 | void = fp.read() |
| 634 | fp.close() |
Guido van Rossum | 3527f59 | 1999-03-29 20:23:41 +0000 | [diff] [blame] | 635 | # In case the server sent a relative URL, join with original: |
Moshe Zadka | 5d87d47 | 2001-04-09 14:54:21 +0000 | [diff] [blame] | 636 | newurl = basejoin(self.type + ":" + url, newurl) |
Guido van Rossum | fa19f7c | 2003-05-16 01:46:51 +0000 | [diff] [blame] | 637 | return self.open(newurl) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 638 | |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 639 | def http_error_301(self, url, fp, errcode, errmsg, headers, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 640 | """Error 301 -- also relocated (permanently).""" |
| 641 | return self.http_error_302(url, fp, errcode, errmsg, headers, data) |
Guido van Rossum | e6ad891 | 1996-09-10 17:02:56 +0000 | [diff] [blame] | 642 | |
Raymond Hettinger | 024aaa1 | 2003-04-24 15:32:12 +0000 | [diff] [blame] | 643 | def http_error_303(self, url, fp, errcode, errmsg, headers, data=None): |
| 644 | """Error 303 -- also relocated (essentially identical to 302).""" |
| 645 | return self.http_error_302(url, fp, errcode, errmsg, headers, data) |
| 646 | |
Guido van Rossum | fa19f7c | 2003-05-16 01:46:51 +0000 | [diff] [blame] | 647 | def http_error_307(self, url, fp, errcode, errmsg, headers, data=None): |
| 648 | """Error 307 -- relocated, but turn POST into error.""" |
| 649 | if data is None: |
| 650 | return self.http_error_302(url, fp, errcode, errmsg, headers, data) |
| 651 | else: |
| 652 | return self.http_error_default(url, fp, errcode, errmsg, headers) |
| 653 | |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 654 | def http_error_401(self, url, fp, errcode, errmsg, headers, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 655 | """Error 401 -- authentication required. |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 656 | This function supports Basic authentication only.""" |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 657 | if not 'www-authenticate' in headers: |
Tim Peters | 85ba673 | 2001-02-28 08:26:44 +0000 | [diff] [blame] | 658 | URLopener.http_error_default(self, url, fp, |
Fred Drake | c680ae8 | 2001-10-13 18:37:07 +0000 | [diff] [blame] | 659 | errcode, errmsg, headers) |
Moshe Zadka | e99bd17 | 2001-02-27 06:27:04 +0000 | [diff] [blame] | 660 | stuff = headers['www-authenticate'] |
| 661 | import re |
| 662 | match = re.match('[ \t]*([^ \t]+)[ \t]+realm="([^"]*)"', stuff) |
| 663 | if not match: |
Tim Peters | 85ba673 | 2001-02-28 08:26:44 +0000 | [diff] [blame] | 664 | URLopener.http_error_default(self, url, fp, |
Moshe Zadka | e99bd17 | 2001-02-27 06:27:04 +0000 | [diff] [blame] | 665 | errcode, errmsg, headers) |
| 666 | scheme, realm = match.groups() |
| 667 | if scheme.lower() != 'basic': |
Tim Peters | 85ba673 | 2001-02-28 08:26:44 +0000 | [diff] [blame] | 668 | URLopener.http_error_default(self, url, fp, |
Moshe Zadka | e99bd17 | 2001-02-27 06:27:04 +0000 | [diff] [blame] | 669 | errcode, errmsg, headers) |
| 670 | name = 'retry_' + self.type + '_basic_auth' |
| 671 | if data is None: |
| 672 | return getattr(self,name)(url, realm) |
| 673 | else: |
| 674 | return getattr(self,name)(url, realm, data) |
Tim Peters | 92037a1 | 2006-01-24 22:44:08 +0000 | [diff] [blame] | 675 | |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 676 | def http_error_407(self, url, fp, errcode, errmsg, headers, data=None): |
| 677 | """Error 407 -- proxy authentication required. |
| 678 | This function supports Basic authentication only.""" |
| 679 | if not 'proxy-authenticate' in headers: |
| 680 | URLopener.http_error_default(self, url, fp, |
| 681 | errcode, errmsg, headers) |
| 682 | stuff = headers['proxy-authenticate'] |
| 683 | import re |
| 684 | match = re.match('[ \t]*([^ \t]+)[ \t]+realm="([^"]*)"', stuff) |
| 685 | if not match: |
| 686 | URLopener.http_error_default(self, url, fp, |
| 687 | errcode, errmsg, headers) |
| 688 | scheme, realm = match.groups() |
| 689 | if scheme.lower() != 'basic': |
| 690 | URLopener.http_error_default(self, url, fp, |
| 691 | errcode, errmsg, headers) |
| 692 | name = 'retry_proxy_' + self.type + '_basic_auth' |
| 693 | if data is None: |
| 694 | return getattr(self,name)(url, realm) |
| 695 | else: |
| 696 | return getattr(self,name)(url, realm, data) |
Tim Peters | 92037a1 | 2006-01-24 22:44:08 +0000 | [diff] [blame] | 697 | |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 698 | def retry_proxy_http_basic_auth(self, url, realm, data=None): |
| 699 | host, selector = splithost(url) |
| 700 | newurl = 'http://' + host + selector |
| 701 | proxy = self.proxies['http'] |
| 702 | urltype, proxyhost = splittype(proxy) |
| 703 | proxyhost, proxyselector = splithost(proxyhost) |
| 704 | i = proxyhost.find('@') + 1 |
| 705 | proxyhost = proxyhost[i:] |
| 706 | user, passwd = self.get_user_passwd(proxyhost, realm, i) |
| 707 | if not (user or passwd): return None |
| 708 | proxyhost = quote(user, safe='') + ':' + quote(passwd, safe='') + '@' + proxyhost |
| 709 | self.proxies['http'] = 'http://' + proxyhost + proxyselector |
| 710 | if data is None: |
| 711 | return self.open(newurl) |
| 712 | else: |
| 713 | return self.open(newurl, data) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 714 | |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 715 | def retry_proxy_https_basic_auth(self, url, realm, data=None): |
| 716 | host, selector = splithost(url) |
| 717 | newurl = 'https://' + host + selector |
| 718 | proxy = self.proxies['https'] |
| 719 | urltype, proxyhost = splittype(proxy) |
| 720 | proxyhost, proxyselector = splithost(proxyhost) |
| 721 | i = proxyhost.find('@') + 1 |
| 722 | proxyhost = proxyhost[i:] |
| 723 | user, passwd = self.get_user_passwd(proxyhost, realm, i) |
| 724 | if not (user or passwd): return None |
| 725 | proxyhost = quote(user, safe='') + ':' + quote(passwd, safe='') + '@' + proxyhost |
| 726 | self.proxies['https'] = 'https://' + proxyhost + proxyselector |
| 727 | if data is None: |
| 728 | return self.open(newurl) |
| 729 | else: |
| 730 | return self.open(newurl, data) |
Tim Peters | 92037a1 | 2006-01-24 22:44:08 +0000 | [diff] [blame] | 731 | |
Guido van Rossum | 3c8baed | 2000-02-01 23:36:55 +0000 | [diff] [blame] | 732 | def retry_http_basic_auth(self, url, realm, data=None): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 733 | host, selector = splithost(url) |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 734 | i = host.find('@') + 1 |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 735 | host = host[i:] |
| 736 | user, passwd = self.get_user_passwd(host, realm, i) |
| 737 | if not (user or passwd): return None |
Guido van Rossum | afc4f04 | 2001-01-15 18:31:13 +0000 | [diff] [blame] | 738 | host = quote(user, safe='') + ':' + quote(passwd, safe='') + '@' + host |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 739 | newurl = 'http://' + host + selector |
Guido van Rossum | 3c8baed | 2000-02-01 23:36:55 +0000 | [diff] [blame] | 740 | if data is None: |
| 741 | return self.open(newurl) |
| 742 | else: |
| 743 | return self.open(newurl, data) |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 744 | |
Guido van Rossum | 3c8baed | 2000-02-01 23:36:55 +0000 | [diff] [blame] | 745 | def retry_https_basic_auth(self, url, realm, data=None): |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 746 | host, selector = splithost(url) |
| 747 | i = host.find('@') + 1 |
| 748 | host = host[i:] |
| 749 | user, passwd = self.get_user_passwd(host, realm, i) |
| 750 | if not (user or passwd): return None |
Guido van Rossum | afc4f04 | 2001-01-15 18:31:13 +0000 | [diff] [blame] | 751 | host = quote(user, safe='') + ':' + quote(passwd, safe='') + '@' + host |
Martin v. Löwis | 3e86595 | 2006-01-24 15:51:21 +0000 | [diff] [blame] | 752 | newurl = 'https://' + host + selector |
| 753 | if data is None: |
| 754 | return self.open(newurl) |
| 755 | else: |
| 756 | return self.open(newurl, data) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 757 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 758 | def get_user_passwd(self, host, realm, clear_cache = 0): |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 759 | key = realm + '@' + host.lower() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 760 | if key in self.auth_cache: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 761 | if clear_cache: |
| 762 | del self.auth_cache[key] |
| 763 | else: |
| 764 | return self.auth_cache[key] |
| 765 | user, passwd = self.prompt_user_passwd(host, realm) |
| 766 | if user or passwd: self.auth_cache[key] = (user, passwd) |
| 767 | return user, passwd |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 768 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 769 | def prompt_user_passwd(self, host, realm): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 770 | """Override this in a GUI environment!""" |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 771 | import getpass, sys |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 772 | try: |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 773 | sys.stdout.write("Enter username for %s at %s: " % (realm, host)) |
| 774 | sys.stdout.flush() |
| 775 | user = sys.stdin.readline() |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 776 | passwd = getpass.getpass("Enter password for %s in %s at %s: " % |
| 777 | (user, realm, host)) |
| 778 | return user, passwd |
| 779 | except KeyboardInterrupt: |
| 780 | print |
| 781 | return None, None |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 782 | |
| 783 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 784 | # Utility functions |
| 785 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 786 | _localhost = None |
| 787 | def localhost(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 788 | """Return the IP address of the magic hostname 'localhost'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 789 | global _localhost |
Raymond Hettinger | 10ff706 | 2002-06-02 03:04:52 +0000 | [diff] [blame] | 790 | if _localhost is None: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 791 | _localhost = socket.gethostbyname('localhost') |
| 792 | return _localhost |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 793 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 794 | _thishost = None |
| 795 | def thishost(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 796 | """Return the IP address of the current host.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 797 | global _thishost |
Raymond Hettinger | 10ff706 | 2002-06-02 03:04:52 +0000 | [diff] [blame] | 798 | if _thishost is None: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 799 | _thishost = socket.gethostbyname(socket.gethostname()) |
| 800 | return _thishost |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 801 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 802 | _ftperrors = None |
| 803 | def ftperrors(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 804 | """Return the set of errors raised by the FTP class.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 805 | global _ftperrors |
Raymond Hettinger | 10ff706 | 2002-06-02 03:04:52 +0000 | [diff] [blame] | 806 | if _ftperrors is None: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 807 | import ftplib |
| 808 | _ftperrors = ftplib.all_errors |
| 809 | return _ftperrors |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 810 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 811 | _noheaders = None |
| 812 | def noheaders(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 813 | """Return an empty mimetools.Message object.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 814 | global _noheaders |
Raymond Hettinger | 10ff706 | 2002-06-02 03:04:52 +0000 | [diff] [blame] | 815 | if _noheaders is None: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 816 | import mimetools |
Raymond Hettinger | a617271 | 2004-12-31 19:15:26 +0000 | [diff] [blame] | 817 | try: |
| 818 | from cStringIO import StringIO |
| 819 | except ImportError: |
| 820 | from StringIO import StringIO |
| 821 | _noheaders = mimetools.Message(StringIO(), 0) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 822 | _noheaders.fp.close() # Recycle file descriptor |
| 823 | return _noheaders |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 824 | |
| 825 | |
| 826 | # Utility classes |
| 827 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 828 | class ftpwrapper: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 829 | """Class used by open_ftp() for cache of open FTP connections.""" |
| 830 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 831 | def __init__(self, user, passwd, host, port, dirs): |
| 832 | self.user = user |
| 833 | self.passwd = passwd |
| 834 | self.host = host |
| 835 | self.port = port |
| 836 | self.dirs = dirs |
| 837 | self.init() |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 838 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 839 | def init(self): |
| 840 | import ftplib |
| 841 | self.busy = 0 |
| 842 | self.ftp = ftplib.FTP() |
| 843 | self.ftp.connect(self.host, self.port) |
| 844 | self.ftp.login(self.user, self.passwd) |
| 845 | for dir in self.dirs: |
| 846 | self.ftp.cwd(dir) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 847 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 848 | def retrfile(self, file, type): |
| 849 | import ftplib |
| 850 | self.endtransfer() |
| 851 | if type in ('d', 'D'): cmd = 'TYPE A'; isdir = 1 |
| 852 | else: cmd = 'TYPE ' + type; isdir = 0 |
| 853 | try: |
| 854 | self.ftp.voidcmd(cmd) |
| 855 | except ftplib.all_errors: |
| 856 | self.init() |
| 857 | self.ftp.voidcmd(cmd) |
| 858 | conn = None |
| 859 | if file and not isdir: |
| 860 | # Use nlst to see if the file exists at all |
| 861 | try: |
| 862 | self.ftp.nlst(file) |
| 863 | except ftplib.error_perm, reason: |
| 864 | raise IOError, ('ftp error', reason), sys.exc_info()[2] |
| 865 | # Restore the transfer mode! |
| 866 | self.ftp.voidcmd(cmd) |
| 867 | # Try to retrieve as a file |
| 868 | try: |
| 869 | cmd = 'RETR ' + file |
| 870 | conn = self.ftp.ntransfercmd(cmd) |
| 871 | except ftplib.error_perm, reason: |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 872 | if str(reason)[:3] != '550': |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 873 | raise IOError, ('ftp error', reason), sys.exc_info()[2] |
| 874 | if not conn: |
| 875 | # Set transfer mode to ASCII! |
| 876 | self.ftp.voidcmd('TYPE A') |
| 877 | # Try a directory listing |
| 878 | if file: cmd = 'LIST ' + file |
| 879 | else: cmd = 'LIST' |
| 880 | conn = self.ftp.ntransfercmd(cmd) |
| 881 | self.busy = 1 |
| 882 | # Pass back both a suitably decorated object and a retrieval length |
| 883 | return (addclosehook(conn[0].makefile('rb'), |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 884 | self.endtransfer), conn[1]) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 885 | def endtransfer(self): |
| 886 | if not self.busy: |
| 887 | return |
| 888 | self.busy = 0 |
| 889 | try: |
| 890 | self.ftp.voidresp() |
| 891 | except ftperrors(): |
| 892 | pass |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 893 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 894 | def close(self): |
| 895 | self.endtransfer() |
| 896 | try: |
| 897 | self.ftp.close() |
| 898 | except ftperrors(): |
| 899 | pass |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 900 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 901 | class addbase: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 902 | """Base class for addinfo and addclosehook.""" |
| 903 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 904 | def __init__(self, fp): |
| 905 | self.fp = fp |
| 906 | self.read = self.fp.read |
| 907 | self.readline = self.fp.readline |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 908 | if hasattr(self.fp, "readlines"): self.readlines = self.fp.readlines |
Georg Brandl | 1f66357 | 2005-11-26 16:50:44 +0000 | [diff] [blame] | 909 | if hasattr(self.fp, "fileno"): |
| 910 | self.fileno = self.fp.fileno |
| 911 | else: |
| 912 | self.fileno = lambda: None |
Raymond Hettinger | 42182eb | 2003-03-09 05:33:33 +0000 | [diff] [blame] | 913 | if hasattr(self.fp, "__iter__"): |
| 914 | self.__iter__ = self.fp.__iter__ |
| 915 | if hasattr(self.fp, "next"): |
| 916 | self.next = self.fp.next |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 917 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 918 | def __repr__(self): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 919 | return '<%s at %r whose fp = %r>' % (self.__class__.__name__, |
| 920 | id(self), self.fp) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 921 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 922 | def close(self): |
| 923 | self.read = None |
| 924 | self.readline = None |
| 925 | self.readlines = None |
| 926 | self.fileno = None |
| 927 | if self.fp: self.fp.close() |
| 928 | self.fp = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 929 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 930 | class addclosehook(addbase): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 931 | """Class to add a close hook to an open file.""" |
| 932 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 933 | def __init__(self, fp, closehook, *hookargs): |
| 934 | addbase.__init__(self, fp) |
| 935 | self.closehook = closehook |
| 936 | self.hookargs = hookargs |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 937 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 938 | def close(self): |
Guido van Rossum | c580dae | 2000-05-24 13:21:46 +0000 | [diff] [blame] | 939 | addbase.close(self) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 940 | if self.closehook: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 941 | self.closehook(*self.hookargs) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 942 | self.closehook = None |
| 943 | self.hookargs = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 944 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 945 | class addinfo(addbase): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 946 | """class to add an info() method to an open file.""" |
| 947 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 948 | def __init__(self, fp, headers): |
| 949 | addbase.__init__(self, fp) |
| 950 | self.headers = headers |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 951 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 952 | def info(self): |
| 953 | return self.headers |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 954 | |
Guido van Rossum | e6ad891 | 1996-09-10 17:02:56 +0000 | [diff] [blame] | 955 | class addinfourl(addbase): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 956 | """class to add info() and geturl() methods to an open file.""" |
| 957 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 958 | def __init__(self, fp, headers, url): |
| 959 | addbase.__init__(self, fp) |
| 960 | self.headers = headers |
| 961 | self.url = url |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 962 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 963 | def info(self): |
| 964 | return self.headers |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 965 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 966 | def geturl(self): |
| 967 | return self.url |
Guido van Rossum | e6ad891 | 1996-09-10 17:02:56 +0000 | [diff] [blame] | 968 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 969 | |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 970 | # Utilities to parse URLs (most of these return None for missing parts): |
Sjoerd Mullender | e0371b8 | 1995-11-10 10:36:07 +0000 | [diff] [blame] | 971 | # unwrap('<URL:type://host/path>') --> 'type://host/path' |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 972 | # splittype('type:opaquestring') --> 'type', 'opaquestring' |
| 973 | # splithost('//host[:port]/path') --> 'host[:port]', '/path' |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 974 | # splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]' |
| 975 | # splitpasswd('user:passwd') -> 'user', 'passwd' |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 976 | # splitport('host:port') --> 'host', 'port' |
| 977 | # splitquery('/path?query') --> '/path', 'query' |
| 978 | # splittag('/path#tag') --> '/path', 'tag' |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 979 | # splitattr('/path;attr1=value1;attr2=value2;...') -> |
| 980 | # '/path', ['attr1=value1', 'attr2=value2', ...] |
| 981 | # splitvalue('attr=value') --> 'attr', 'value' |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 982 | # splitgophertype('/Xselector') --> 'X', 'selector' |
| 983 | # unquote('abc%20def') -> 'abc def' |
| 984 | # quote('abc def') -> 'abc%20def') |
| 985 | |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 986 | try: |
| 987 | unicode |
| 988 | except NameError: |
Guido van Rossum | 4b46c0a | 2002-05-24 17:58:05 +0000 | [diff] [blame] | 989 | def _is_unicode(x): |
| 990 | return 0 |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 991 | else: |
| 992 | def _is_unicode(x): |
| 993 | return isinstance(x, unicode) |
Guido van Rossum | 4b46c0a | 2002-05-24 17:58:05 +0000 | [diff] [blame] | 994 | |
Martin v. Löwis | 1d99433 | 2000-12-03 18:30:10 +0000 | [diff] [blame] | 995 | def toBytes(url): |
| 996 | """toBytes(u"URL") --> 'URL'.""" |
| 997 | # Most URL schemes require ASCII. If that changes, the conversion |
| 998 | # can be relaxed |
Guido van Rossum | 4b46c0a | 2002-05-24 17:58:05 +0000 | [diff] [blame] | 999 | if _is_unicode(url): |
Martin v. Löwis | 1d99433 | 2000-12-03 18:30:10 +0000 | [diff] [blame] | 1000 | try: |
| 1001 | url = url.encode("ASCII") |
| 1002 | except UnicodeError: |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1003 | raise UnicodeError("URL " + repr(url) + |
| 1004 | " contains non-ASCII characters") |
Martin v. Löwis | 1d99433 | 2000-12-03 18:30:10 +0000 | [diff] [blame] | 1005 | return url |
| 1006 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1007 | def unwrap(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1008 | """unwrap('<URL:type://host/path>') --> 'type://host/path'.""" |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1009 | url = url.strip() |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1010 | if url[:1] == '<' and url[-1:] == '>': |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1011 | url = url[1:-1].strip() |
| 1012 | if url[:4] == 'URL:': url = url[4:].strip() |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1013 | return url |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1014 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1015 | _typeprog = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1016 | def splittype(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1017 | """splittype('type:opaquestring') --> 'type', 'opaquestring'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1018 | global _typeprog |
| 1019 | if _typeprog is None: |
| 1020 | import re |
| 1021 | _typeprog = re.compile('^([^/:]+):') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1022 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1023 | match = _typeprog.match(url) |
| 1024 | if match: |
| 1025 | scheme = match.group(1) |
Fred Drake | 9e94afd | 2000-07-01 07:03:30 +0000 | [diff] [blame] | 1026 | return scheme.lower(), url[len(scheme) + 1:] |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1027 | return None, url |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1028 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1029 | _hostprog = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1030 | def splithost(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1031 | """splithost('//host[:port]/path') --> 'host[:port]', '/path'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1032 | global _hostprog |
| 1033 | if _hostprog is None: |
| 1034 | import re |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 1035 | _hostprog = re.compile('^//([^/?]*)(.*)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1036 | |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 1037 | match = _hostprog.match(url) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1038 | if match: return match.group(1, 2) |
| 1039 | return None, url |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1040 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1041 | _userprog = None |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 1042 | def splituser(host): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1043 | """splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1044 | global _userprog |
| 1045 | if _userprog is None: |
| 1046 | import re |
Raymond Hettinger | f2e45dd | 2002-08-18 20:08:56 +0000 | [diff] [blame] | 1047 | _userprog = re.compile('^(.*)@(.*)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1048 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1049 | match = _userprog.match(host) |
Fred Drake | 567ca8e | 2000-08-21 21:42:42 +0000 | [diff] [blame] | 1050 | if match: return map(unquote, match.group(1, 2)) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1051 | return None, host |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 1052 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1053 | _passwdprog = None |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 1054 | def splitpasswd(user): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1055 | """splitpasswd('user:passwd') -> 'user', 'passwd'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1056 | global _passwdprog |
| 1057 | if _passwdprog is None: |
| 1058 | import re |
| 1059 | _passwdprog = re.compile('^([^:]*):(.*)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1060 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1061 | match = _passwdprog.match(user) |
| 1062 | if match: return match.group(1, 2) |
| 1063 | return user, None |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 1064 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1065 | # splittag('/path#tag') --> '/path', 'tag' |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1066 | _portprog = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1067 | def splitport(host): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1068 | """splitport('host:port') --> 'host', 'port'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1069 | global _portprog |
| 1070 | if _portprog is None: |
| 1071 | import re |
| 1072 | _portprog = re.compile('^(.*):([0-9]+)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1073 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1074 | match = _portprog.match(host) |
| 1075 | if match: return match.group(1, 2) |
| 1076 | return host, None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1077 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1078 | _nportprog = None |
Guido van Rossum | 53725a2 | 1996-06-13 19:12:35 +0000 | [diff] [blame] | 1079 | def splitnport(host, defport=-1): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1080 | """Split host and port, returning numeric port. |
| 1081 | Return given default port if no ':' found; defaults to -1. |
| 1082 | Return numerical port if a valid number are found after ':'. |
| 1083 | Return None if ':' but not a valid number.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1084 | global _nportprog |
| 1085 | if _nportprog is None: |
| 1086 | import re |
| 1087 | _nportprog = re.compile('^(.*):(.*)$') |
Guido van Rossum | 7e7ca0b | 1998-03-26 21:01:39 +0000 | [diff] [blame] | 1088 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1089 | match = _nportprog.match(host) |
| 1090 | if match: |
| 1091 | host, port = match.group(1, 2) |
| 1092 | try: |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1093 | if not port: raise ValueError, "no digits" |
| 1094 | nport = int(port) |
| 1095 | except ValueError: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1096 | nport = None |
| 1097 | return host, nport |
| 1098 | return host, defport |
Guido van Rossum | 53725a2 | 1996-06-13 19:12:35 +0000 | [diff] [blame] | 1099 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1100 | _queryprog = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1101 | def splitquery(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1102 | """splitquery('/path?query') --> '/path', 'query'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1103 | global _queryprog |
| 1104 | if _queryprog is None: |
| 1105 | import re |
| 1106 | _queryprog = re.compile('^(.*)\?([^?]*)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1107 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1108 | match = _queryprog.match(url) |
| 1109 | if match: return match.group(1, 2) |
| 1110 | return url, None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1112 | _tagprog = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1113 | def splittag(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1114 | """splittag('/path#tag') --> '/path', 'tag'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1115 | global _tagprog |
| 1116 | if _tagprog is None: |
| 1117 | import re |
| 1118 | _tagprog = re.compile('^(.*)#([^#]*)$') |
Guido van Rossum | 7e7ca0b | 1998-03-26 21:01:39 +0000 | [diff] [blame] | 1119 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1120 | match = _tagprog.match(url) |
| 1121 | if match: return match.group(1, 2) |
| 1122 | return url, None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1123 | |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 1124 | def splitattr(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1125 | """splitattr('/path;attr1=value1;attr2=value2;...') -> |
| 1126 | '/path', ['attr1=value1', 'attr2=value2', ...].""" |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1127 | words = url.split(';') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1128 | return words[0], words[1:] |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 1129 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1130 | _valueprog = None |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 1131 | def splitvalue(attr): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1132 | """splitvalue('attr=value') --> 'attr', 'value'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1133 | global _valueprog |
| 1134 | if _valueprog is None: |
| 1135 | import re |
| 1136 | _valueprog = re.compile('^([^=]*)=(.*)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 1137 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1138 | match = _valueprog.match(attr) |
| 1139 | if match: return match.group(1, 2) |
| 1140 | return attr, None |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 1141 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1142 | def splitgophertype(selector): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1143 | """splitgophertype('/Xselector') --> 'X', 'selector'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1144 | if selector[:1] == '/' and selector[1:2]: |
| 1145 | return selector[1], selector[2:] |
| 1146 | return None, selector |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1147 | |
Raymond Hettinger | 803ce80 | 2005-09-10 06:49:04 +0000 | [diff] [blame] | 1148 | _hextochr = dict(('%02x' % i, chr(i)) for i in range(256)) |
| 1149 | _hextochr.update(('%02X' % i, chr(i)) for i in range(256)) |
| 1150 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1151 | def unquote(s): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1152 | """unquote('abc%20def') -> 'abc def'.""" |
Raymond Hettinger | 803ce80 | 2005-09-10 06:49:04 +0000 | [diff] [blame] | 1153 | res = s.split('%') |
| 1154 | for i in xrange(1, len(res)): |
| 1155 | item = res[i] |
| 1156 | try: |
| 1157 | res[i] = _hextochr[item[:2]] + item[2:] |
| 1158 | except KeyError: |
| 1159 | res[i] = '%' + item |
Raymond Hettinger | 4b0f20d | 2005-10-15 16:41:53 +0000 | [diff] [blame] | 1160 | except UnicodeDecodeError: |
| 1161 | res[i] = unichr(int(item[:2], 16)) + item[2:] |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1162 | return "".join(res) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1163 | |
Guido van Rossum | 0564e12 | 1996-12-13 14:47:36 +0000 | [diff] [blame] | 1164 | def unquote_plus(s): |
Skip Montanaro | 79f1c17 | 2000-08-22 03:00:52 +0000 | [diff] [blame] | 1165 | """unquote('%7e/abc+def') -> '~/abc def'""" |
Brett Cannon | aaeffaf | 2004-03-23 23:50:17 +0000 | [diff] [blame] | 1166 | s = s.replace('+', ' ') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1167 | return unquote(s) |
Guido van Rossum | 0564e12 | 1996-12-13 14:47:36 +0000 | [diff] [blame] | 1168 | |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 1169 | always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 1170 | 'abcdefghijklmnopqrstuvwxyz' |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 1171 | '0123456789' '_.-') |
Raymond Hettinger | 199d2f7 | 2005-09-09 22:27:13 +0000 | [diff] [blame] | 1172 | _safemaps = {} |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 1173 | |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 1174 | def quote(s, safe = '/'): |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 1175 | """quote('abc def') -> 'abc%20def' |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 1176 | |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 1177 | Each part of a URL, e.g. the path info, the query, etc., has a |
| 1178 | different set of reserved characters that must be quoted. |
| 1179 | |
| 1180 | RFC 2396 Uniform Resource Identifiers (URI): Generic Syntax lists |
| 1181 | the following reserved characters. |
| 1182 | |
| 1183 | reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | |
| 1184 | "$" | "," |
| 1185 | |
| 1186 | Each of these characters is reserved in some component of a URL, |
| 1187 | but not necessarily in all of them. |
| 1188 | |
| 1189 | By default, the quote function is intended for quoting the path |
| 1190 | section of a URL. Thus, it will not encode '/'. This character |
| 1191 | is reserved, but in typical usage the quote function is being |
| 1192 | called on a path where the existing slash characters are used as |
| 1193 | reserved characters. |
| 1194 | """ |
Raymond Hettinger | 199d2f7 | 2005-09-09 22:27:13 +0000 | [diff] [blame] | 1195 | cachekey = (safe, always_safe) |
| 1196 | try: |
| 1197 | safe_map = _safemaps[cachekey] |
| 1198 | except KeyError: |
| 1199 | safe += always_safe |
| 1200 | safe_map = {} |
| 1201 | for i in range(256): |
| 1202 | c = chr(i) |
| 1203 | safe_map[c] = (c in safe) and c or ('%%%02X' % i) |
| 1204 | _safemaps[cachekey] = safe_map |
| 1205 | res = map(safe_map.__getitem__, s) |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1206 | return ''.join(res) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1207 | |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 1208 | def quote_plus(s, safe = ''): |
| 1209 | """Quote the query fragment of a URL; replacing ' ' with '+'""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1210 | if ' ' in s: |
Raymond Hettinger | cf6b632 | 2005-09-10 18:17:54 +0000 | [diff] [blame] | 1211 | s = quote(s, safe + ' ') |
| 1212 | return s.replace(' ', '+') |
| 1213 | return quote(s, safe) |
Guido van Rossum | 0564e12 | 1996-12-13 14:47:36 +0000 | [diff] [blame] | 1214 | |
Skip Montanaro | 14f1ad4 | 2001-01-28 21:11:12 +0000 | [diff] [blame] | 1215 | def urlencode(query,doseq=0): |
| 1216 | """Encode a sequence of two-element tuples or dictionary into a URL query string. |
Skip Montanaro | a5d23a1 | 2001-01-20 15:56:39 +0000 | [diff] [blame] | 1217 | |
Skip Montanaro | 14f1ad4 | 2001-01-28 21:11:12 +0000 | [diff] [blame] | 1218 | If any values in the query arg are sequences and doseq is true, each |
Skip Montanaro | a5d23a1 | 2001-01-20 15:56:39 +0000 | [diff] [blame] | 1219 | sequence element is converted to a separate parameter. |
Skip Montanaro | 14f1ad4 | 2001-01-28 21:11:12 +0000 | [diff] [blame] | 1220 | |
| 1221 | If the query arg is a sequence of two-element tuples, the order of the |
| 1222 | parameters in the output will match the order of parameters in the |
| 1223 | input. |
Skip Montanaro | a5d23a1 | 2001-01-20 15:56:39 +0000 | [diff] [blame] | 1224 | """ |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 1225 | |
Skip Montanaro | 14f1ad4 | 2001-01-28 21:11:12 +0000 | [diff] [blame] | 1226 | if hasattr(query,"items"): |
| 1227 | # mapping objects |
| 1228 | query = query.items() |
| 1229 | else: |
| 1230 | # it's a bother at times that strings and string-like objects are |
| 1231 | # sequences... |
| 1232 | try: |
| 1233 | # non-sequence items should not work with len() |
Skip Montanaro | 14f1ad4 | 2001-01-28 21:11:12 +0000 | [diff] [blame] | 1234 | # non-empty strings will fail this |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 1235 | if len(query) and not isinstance(query[0], tuple): |
Skip Montanaro | 14f1ad4 | 2001-01-28 21:11:12 +0000 | [diff] [blame] | 1236 | raise TypeError |
| 1237 | # zero-length sequences of all types will get here and succeed, |
| 1238 | # but that's a minor nit - since the original implementation |
| 1239 | # allowed empty dicts that type of behavior probably should be |
| 1240 | # preserved for consistency |
| 1241 | except TypeError: |
| 1242 | ty,va,tb = sys.exc_info() |
| 1243 | raise TypeError, "not a valid non-string sequence or mapping object", tb |
| 1244 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1245 | l = [] |
Skip Montanaro | a5d23a1 | 2001-01-20 15:56:39 +0000 | [diff] [blame] | 1246 | if not doseq: |
| 1247 | # preserve old behavior |
Skip Montanaro | 14f1ad4 | 2001-01-28 21:11:12 +0000 | [diff] [blame] | 1248 | for k, v in query: |
Skip Montanaro | a5d23a1 | 2001-01-20 15:56:39 +0000 | [diff] [blame] | 1249 | k = quote_plus(str(k)) |
| 1250 | v = quote_plus(str(v)) |
| 1251 | l.append(k + '=' + v) |
| 1252 | else: |
Skip Montanaro | 14f1ad4 | 2001-01-28 21:11:12 +0000 | [diff] [blame] | 1253 | for k, v in query: |
Skip Montanaro | a5d23a1 | 2001-01-20 15:56:39 +0000 | [diff] [blame] | 1254 | k = quote_plus(str(k)) |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 1255 | if isinstance(v, str): |
Skip Montanaro | a5d23a1 | 2001-01-20 15:56:39 +0000 | [diff] [blame] | 1256 | v = quote_plus(v) |
| 1257 | l.append(k + '=' + v) |
Guido van Rossum | 4b46c0a | 2002-05-24 17:58:05 +0000 | [diff] [blame] | 1258 | elif _is_unicode(v): |
Skip Montanaro | a5d23a1 | 2001-01-20 15:56:39 +0000 | [diff] [blame] | 1259 | # is there a reasonable way to convert to ASCII? |
| 1260 | # encode generates a string, but "replace" or "ignore" |
| 1261 | # lose information and "strict" can raise UnicodeError |
| 1262 | v = quote_plus(v.encode("ASCII","replace")) |
| 1263 | l.append(k + '=' + v) |
| 1264 | else: |
| 1265 | try: |
| 1266 | # is this a sufficient test for sequence-ness? |
| 1267 | x = len(v) |
| 1268 | except TypeError: |
| 1269 | # not a sequence |
| 1270 | v = quote_plus(str(v)) |
| 1271 | l.append(k + '=' + v) |
| 1272 | else: |
| 1273 | # loop over the sequence |
| 1274 | for elt in v: |
| 1275 | l.append(k + '=' + quote_plus(str(elt))) |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1276 | return '&'.join(l) |
Guido van Rossum | 810a339 | 1998-07-22 21:33:23 +0000 | [diff] [blame] | 1277 | |
Guido van Rossum | 442e720 | 1996-03-20 15:33:11 +0000 | [diff] [blame] | 1278 | # Proxy handling |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1279 | def getproxies_environment(): |
| 1280 | """Return a dictionary of scheme -> proxy server URL mappings. |
| 1281 | |
| 1282 | Scan the environment for variables named <scheme>_proxy; |
| 1283 | this seems to be the standard convention. If you need a |
| 1284 | different way, you can pass a proxies dictionary to the |
| 1285 | [Fancy]URLopener constructor. |
| 1286 | |
| 1287 | """ |
| 1288 | proxies = {} |
| 1289 | for name, value in os.environ.items(): |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1290 | name = name.lower() |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1291 | if value and name[-6:] == '_proxy': |
| 1292 | proxies[name[:-6]] = value |
| 1293 | return proxies |
| 1294 | |
Jack Jansen | 11d9b06 | 2004-07-16 11:45:00 +0000 | [diff] [blame] | 1295 | if sys.platform == 'darwin': |
| 1296 | def getproxies_internetconfig(): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1297 | """Return a dictionary of scheme -> proxy server URL mappings. |
Guido van Rossum | 442e720 | 1996-03-20 15:33:11 +0000 | [diff] [blame] | 1298 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1299 | By convention the mac uses Internet Config to store |
| 1300 | proxies. An HTTP proxy, for instance, is stored under |
| 1301 | the HttpProxy key. |
Guido van Rossum | 442e720 | 1996-03-20 15:33:11 +0000 | [diff] [blame] | 1302 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1303 | """ |
| 1304 | try: |
| 1305 | import ic |
| 1306 | except ImportError: |
| 1307 | return {} |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 1308 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1309 | try: |
| 1310 | config = ic.IC() |
| 1311 | except ic.error: |
| 1312 | return {} |
| 1313 | proxies = {} |
| 1314 | # HTTP: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1315 | if 'UseHTTPProxy' in config and config['UseHTTPProxy']: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1316 | try: |
| 1317 | value = config['HTTPProxyHost'] |
| 1318 | except ic.error: |
| 1319 | pass |
| 1320 | else: |
| 1321 | proxies['http'] = 'http://%s' % value |
| 1322 | # FTP: XXXX To be done. |
| 1323 | # Gopher: XXXX To be done. |
| 1324 | return proxies |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1325 | |
Tim Peters | 55c12d4 | 2001-08-09 18:04:14 +0000 | [diff] [blame] | 1326 | def proxy_bypass(x): |
| 1327 | return 0 |
| 1328 | |
Jack Jansen | 11d9b06 | 2004-07-16 11:45:00 +0000 | [diff] [blame] | 1329 | def getproxies(): |
| 1330 | return getproxies_environment() or getproxies_internetconfig() |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 1331 | |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1332 | elif os.name == 'nt': |
| 1333 | def getproxies_registry(): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1334 | """Return a dictionary of scheme -> proxy server URL mappings. |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1335 | |
| 1336 | Win32 uses the registry to store proxies. |
| 1337 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1338 | """ |
| 1339 | proxies = {} |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1340 | try: |
| 1341 | import _winreg |
| 1342 | except ImportError: |
| 1343 | # Std module, so should be around - but you never know! |
| 1344 | return proxies |
| 1345 | try: |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 1346 | internetSettings = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, |
| 1347 | r'Software\Microsoft\Windows\CurrentVersion\Internet Settings') |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1348 | proxyEnable = _winreg.QueryValueEx(internetSettings, |
| 1349 | 'ProxyEnable')[0] |
| 1350 | if proxyEnable: |
| 1351 | # Returned as Unicode but problems if not converted to ASCII |
| 1352 | proxyServer = str(_winreg.QueryValueEx(internetSettings, |
| 1353 | 'ProxyServer')[0]) |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 1354 | if '=' in proxyServer: |
| 1355 | # Per-protocol settings |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1356 | for p in proxyServer.split(';'): |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 1357 | protocol, address = p.split('=', 1) |
Guido van Rossum | b955d6c | 2002-03-31 23:38:48 +0000 | [diff] [blame] | 1358 | # See if address has a type:// prefix |
Guido van Rossum | 64e5aa9 | 2002-04-02 14:38:16 +0000 | [diff] [blame] | 1359 | import re |
| 1360 | if not re.match('^([^/:]+)://', address): |
Guido van Rossum | b955d6c | 2002-03-31 23:38:48 +0000 | [diff] [blame] | 1361 | address = '%s://%s' % (protocol, address) |
| 1362 | proxies[protocol] = address |
Fredrik Lundh | b49f88b | 2000-09-24 18:51:25 +0000 | [diff] [blame] | 1363 | else: |
| 1364 | # Use one setting for all protocols |
| 1365 | if proxyServer[:5] == 'http:': |
| 1366 | proxies['http'] = proxyServer |
| 1367 | else: |
| 1368 | proxies['http'] = 'http://%s' % proxyServer |
| 1369 | proxies['ftp'] = 'ftp://%s' % proxyServer |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1370 | internetSettings.Close() |
| 1371 | except (WindowsError, ValueError, TypeError): |
| 1372 | # Either registry key not found etc, or the value in an |
| 1373 | # unexpected format. |
| 1374 | # proxies already set up to be empty so nothing to do |
| 1375 | pass |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1376 | return proxies |
Guido van Rossum | 442e720 | 1996-03-20 15:33:11 +0000 | [diff] [blame] | 1377 | |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1378 | def getproxies(): |
| 1379 | """Return a dictionary of scheme -> proxy server URL mappings. |
| 1380 | |
| 1381 | Returns settings gathered from the environment, if specified, |
| 1382 | or the registry. |
| 1383 | |
| 1384 | """ |
| 1385 | return getproxies_environment() or getproxies_registry() |
Tim Peters | 55c12d4 | 2001-08-09 18:04:14 +0000 | [diff] [blame] | 1386 | |
| 1387 | def proxy_bypass(host): |
| 1388 | try: |
| 1389 | import _winreg |
| 1390 | import re |
Tim Peters | 55c12d4 | 2001-08-09 18:04:14 +0000 | [diff] [blame] | 1391 | except ImportError: |
| 1392 | # Std modules, so should be around - but you never know! |
| 1393 | return 0 |
| 1394 | try: |
| 1395 | internetSettings = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, |
| 1396 | r'Software\Microsoft\Windows\CurrentVersion\Internet Settings') |
| 1397 | proxyEnable = _winreg.QueryValueEx(internetSettings, |
| 1398 | 'ProxyEnable')[0] |
| 1399 | proxyOverride = str(_winreg.QueryValueEx(internetSettings, |
| 1400 | 'ProxyOverride')[0]) |
| 1401 | # ^^^^ Returned as Unicode but problems if not converted to ASCII |
| 1402 | except WindowsError: |
| 1403 | return 0 |
| 1404 | if not proxyEnable or not proxyOverride: |
| 1405 | return 0 |
| 1406 | # try to make a host list from name and IP address. |
Georg Brandl | 1f63670 | 2006-02-18 23:10:23 +0000 | [diff] [blame] | 1407 | rawHost, port = splitport(host) |
| 1408 | host = [rawHost] |
Tim Peters | 55c12d4 | 2001-08-09 18:04:14 +0000 | [diff] [blame] | 1409 | try: |
Georg Brandl | 1f63670 | 2006-02-18 23:10:23 +0000 | [diff] [blame] | 1410 | addr = socket.gethostbyname(rawHost) |
| 1411 | if addr != rawHost: |
Tim Peters | 55c12d4 | 2001-08-09 18:04:14 +0000 | [diff] [blame] | 1412 | host.append(addr) |
| 1413 | except socket.error: |
| 1414 | pass |
Georg Brandl | 1f63670 | 2006-02-18 23:10:23 +0000 | [diff] [blame] | 1415 | try: |
| 1416 | fqdn = socket.getfqdn(rawHost) |
| 1417 | if fqdn != rawHost: |
| 1418 | host.append(fqdn) |
| 1419 | except socket.error: |
| 1420 | pass |
Tim Peters | 55c12d4 | 2001-08-09 18:04:14 +0000 | [diff] [blame] | 1421 | # make a check value list from the registry entry: replace the |
| 1422 | # '<local>' string by the localhost entry and the corresponding |
| 1423 | # canonical entry. |
| 1424 | proxyOverride = proxyOverride.split(';') |
| 1425 | i = 0 |
| 1426 | while i < len(proxyOverride): |
| 1427 | if proxyOverride[i] == '<local>': |
| 1428 | proxyOverride[i:i+1] = ['localhost', |
| 1429 | '127.0.0.1', |
| 1430 | socket.gethostname(), |
| 1431 | socket.gethostbyname( |
| 1432 | socket.gethostname())] |
| 1433 | i += 1 |
| 1434 | # print proxyOverride |
| 1435 | # now check if we match one of the registry values. |
| 1436 | for test in proxyOverride: |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 1437 | test = test.replace(".", r"\.") # mask dots |
| 1438 | test = test.replace("*", r".*") # change glob sequence |
| 1439 | test = test.replace("?", r".") # change glob char |
Tim Peters | 55c12d4 | 2001-08-09 18:04:14 +0000 | [diff] [blame] | 1440 | for val in host: |
| 1441 | # print "%s <--> %s" %( test, val ) |
| 1442 | if re.match(test, val, re.I): |
| 1443 | return 1 |
| 1444 | return 0 |
| 1445 | |
Mark Hammond | 4f570b9 | 2000-07-26 07:04:38 +0000 | [diff] [blame] | 1446 | else: |
| 1447 | # By default use environment variables |
| 1448 | getproxies = getproxies_environment |
| 1449 | |
Tim Peters | 55c12d4 | 2001-08-09 18:04:14 +0000 | [diff] [blame] | 1450 | def proxy_bypass(host): |
| 1451 | return 0 |
Guido van Rossum | 442e720 | 1996-03-20 15:33:11 +0000 | [diff] [blame] | 1452 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1453 | # Test and time quote() and unquote() |
| 1454 | def test1(): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1455 | s = '' |
| 1456 | for i in range(256): s = s + chr(i) |
| 1457 | s = s*4 |
| 1458 | t0 = time.time() |
| 1459 | qs = quote(s) |
| 1460 | uqs = unquote(qs) |
| 1461 | t1 = time.time() |
| 1462 | if uqs != s: |
| 1463 | print 'Wrong!' |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1464 | print repr(s) |
| 1465 | print repr(qs) |
| 1466 | print repr(uqs) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1467 | print round(t1 - t0, 3), 'sec' |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1468 | |
| 1469 | |
Guido van Rossum | 9ab96d4 | 1998-09-28 14:07:00 +0000 | [diff] [blame] | 1470 | def reporthook(blocknum, blocksize, totalsize): |
| 1471 | # Report during remote transfers |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1472 | print "Block number: %d, Block size: %d, Total size: %d" % ( |
| 1473 | blocknum, blocksize, totalsize) |
Guido van Rossum | 9ab96d4 | 1998-09-28 14:07:00 +0000 | [diff] [blame] | 1474 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1475 | # Test program |
Guido van Rossum | 2349015 | 1998-06-25 02:39:00 +0000 | [diff] [blame] | 1476 | def test(args=[]): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1477 | if not args: |
| 1478 | args = [ |
| 1479 | '/etc/passwd', |
| 1480 | 'file:/etc/passwd', |
| 1481 | 'file://localhost/etc/passwd', |
Andrew M. Kuchling | 56a4235 | 2002-03-18 22:18:46 +0000 | [diff] [blame] | 1482 | 'ftp://ftp.python.org/pub/python/README', |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1483 | ## 'gopher://gopher.micro.umn.edu/1/', |
| 1484 | 'http://www.python.org/index.html', |
| 1485 | ] |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1486 | if hasattr(URLopener, "open_https"): |
| 1487 | args.append('https://synergy.as.cmu.edu/~geek/') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1488 | try: |
| 1489 | for url in args: |
| 1490 | print '-'*10, url, '-'*10 |
| 1491 | fn, h = urlretrieve(url, None, reporthook) |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1492 | print fn |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1493 | if h: |
| 1494 | print '======' |
| 1495 | for k in h.keys(): print k + ':', h[k] |
| 1496 | print '======' |
| 1497 | fp = open(fn, 'rb') |
| 1498 | data = fp.read() |
| 1499 | del fp |
| 1500 | if '\r' in data: |
| 1501 | table = string.maketrans("", "") |
Guido van Rossum | b2493f8 | 2000-12-15 15:01:37 +0000 | [diff] [blame] | 1502 | data = data.translate(table, "\r") |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1503 | print data |
| 1504 | fn, h = None, None |
| 1505 | print '-'*40 |
| 1506 | finally: |
| 1507 | urlcleanup() |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1508 | |
Guido van Rossum | 2349015 | 1998-06-25 02:39:00 +0000 | [diff] [blame] | 1509 | def main(): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1510 | import getopt, sys |
| 1511 | try: |
| 1512 | opts, args = getopt.getopt(sys.argv[1:], "th") |
| 1513 | except getopt.error, msg: |
| 1514 | print msg |
| 1515 | print "Use -h for help" |
| 1516 | return |
| 1517 | t = 0 |
| 1518 | for o, a in opts: |
| 1519 | if o == '-t': |
| 1520 | t = t + 1 |
| 1521 | if o == '-h': |
| 1522 | print "Usage: python urllib.py [-t] [url ...]" |
| 1523 | print "-t runs self-test;", |
| 1524 | print "otherwise, contents of urls are printed" |
| 1525 | return |
| 1526 | if t: |
| 1527 | if t > 1: |
| 1528 | test1() |
| 1529 | test(args) |
| 1530 | else: |
| 1531 | if not args: |
| 1532 | print "Use -h for help" |
| 1533 | for url in args: |
| 1534 | print urlopen(url).read(), |
Guido van Rossum | 2349015 | 1998-06-25 02:39:00 +0000 | [diff] [blame] | 1535 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1536 | # Run test program when run as a script |
| 1537 | if __name__ == '__main__': |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1538 | main() |