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() |
| 19 | and close() methods work like those of open files. |
| 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 | 3c8484e | 1996-11-20 22:02:24 +0000 | [diff] [blame] | 28 | import sys |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 29 | |
| 30 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 31 | __version__ = '1.12' # XXX This version is not always updated :-( |
Guido van Rossum | f668d17 | 1997-06-06 21:11:11 +0000 | [diff] [blame] | 32 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 33 | MAXFTPCACHE = 10 # Trim the ftp cache beyond this size |
Guido van Rossum | 6cb15a0 | 1995-06-22 19:00:13 +0000 | [diff] [blame] | 34 | |
Jack Jansen | dc3e3f6 | 1995-12-15 13:22:13 +0000 | [diff] [blame] | 35 | # Helper for non-unix systems |
| 36 | if os.name == 'mac': |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 37 | from macurl2path import url2pathname, pathname2url |
Guido van Rossum | 7e7ca0b | 1998-03-26 21:01:39 +0000 | [diff] [blame] | 38 | elif os.name == 'nt': |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 39 | from nturl2path import url2pathname, pathname2url |
Jack Jansen | dc3e3f6 | 1995-12-15 13:22:13 +0000 | [diff] [blame] | 40 | else: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 41 | def url2pathname(pathname): |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 42 | return unquote(pathname) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 43 | def pathname2url(pathname): |
Guido van Rossum | 367ac80 | 1999-03-12 14:31:10 +0000 | [diff] [blame] | 44 | return quote(pathname) |
Guido van Rossum | 33add0a | 1998-12-18 15:25:22 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 46 | # This really consists of two pieces: |
| 47 | # (1) a class which handles opening of all sorts of URLs |
| 48 | # (plus assorted utilities etc.) |
| 49 | # (2) a set of functions for parsing URLs |
| 50 | # XXX Should these be separated out into different modules? |
| 51 | |
| 52 | |
| 53 | # Shortcut for basic usage |
| 54 | _urlopener = None |
Guido van Rossum | bd01374 | 1996-12-10 16:00:28 +0000 | [diff] [blame] | 55 | def urlopen(url, data=None): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 56 | global _urlopener |
| 57 | if not _urlopener: |
| 58 | _urlopener = FancyURLopener() |
| 59 | if data is None: |
| 60 | return _urlopener.open(url) |
| 61 | else: |
| 62 | return _urlopener.open(url, data) |
Guido van Rossum | 9ab96d4 | 1998-09-28 14:07:00 +0000 | [diff] [blame] | 63 | def urlretrieve(url, filename=None, reporthook=None): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 64 | global _urlopener |
| 65 | if not _urlopener: |
| 66 | _urlopener = FancyURLopener() |
| 67 | return _urlopener.retrieve(url, filename, reporthook) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 68 | def urlcleanup(): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 69 | if _urlopener: |
| 70 | _urlopener.cleanup() |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 71 | |
| 72 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 73 | ftpcache = {} |
| 74 | class URLopener: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 75 | """Class to open URLs. |
| 76 | This is a class rather than just a subroutine because we may need |
| 77 | more than one set of global protocol-specific options. |
| 78 | Note -- this is a base class for those who don't want the |
| 79 | automatic handling of errors type 302 (relocated) and 401 |
| 80 | (authorization needed).""" |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 81 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 82 | __tempfiles = None |
Guido van Rossum | 29e7781 | 1996-11-27 19:39:58 +0000 | [diff] [blame] | 83 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 84 | # Constructor |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 85 | def __init__(self, proxies=None, **x509): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 86 | if proxies is None: |
| 87 | proxies = getproxies() |
| 88 | assert hasattr(proxies, 'has_key'), "proxies must be a mapping" |
| 89 | self.proxies = proxies |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 90 | self.key_file = x509.get('key_file') |
| 91 | self.cert_file = x509.get('cert_file') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 92 | server_version = "Python-urllib/%s" % __version__ |
| 93 | self.addheaders = [('User-agent', server_version)] |
| 94 | self.__tempfiles = [] |
| 95 | self.__unlink = os.unlink # See cleanup() |
| 96 | self.tempcache = None |
| 97 | # Undocumented feature: if you assign {} to tempcache, |
| 98 | # it is used to cache files retrieved with |
| 99 | # self.retrieve(). This is not enabled by default |
| 100 | # since it does not work for changing documents (and I |
| 101 | # haven't got the logic to check expiration headers |
| 102 | # yet). |
| 103 | self.ftpcache = ftpcache |
| 104 | # Undocumented feature: you can use a different |
| 105 | # ftp cache by assigning to the .ftpcache member; |
| 106 | # in case you want logically independent URL openers |
| 107 | # XXX This is not threadsafe. Bah. |
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 | def __del__(self): |
| 110 | self.close() |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 111 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 112 | def close(self): |
| 113 | self.cleanup() |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 114 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 115 | def cleanup(self): |
| 116 | # This code sometimes runs when the rest of this module |
| 117 | # has already been deleted, so it can't use any globals |
| 118 | # or import anything. |
| 119 | if self.__tempfiles: |
| 120 | for file in self.__tempfiles: |
| 121 | try: |
| 122 | self.__unlink(file) |
| 123 | except: |
| 124 | pass |
| 125 | del self.__tempfiles[:] |
| 126 | if self.tempcache: |
| 127 | self.tempcache.clear() |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 128 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 129 | def addheader(self, *args): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 130 | """Add a header to be used by the HTTP interface only |
| 131 | e.g. u.addheader('Accept', 'sound/basic')""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 132 | self.addheaders.append(args) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 133 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 134 | # External interface |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 135 | def open(self, fullurl, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 136 | """Use URLopener().open(file) instead of open(file, 'r').""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 137 | fullurl = unwrap(fullurl) |
| 138 | if self.tempcache and self.tempcache.has_key(fullurl): |
| 139 | filename, headers = self.tempcache[fullurl] |
| 140 | fp = open(filename, 'rb') |
| 141 | return addinfourl(fp, headers, fullurl) |
| 142 | type, url = splittype(fullurl) |
| 143 | if not type: type = 'file' |
| 144 | if self.proxies.has_key(type): |
| 145 | proxy = self.proxies[type] |
| 146 | type, proxy = splittype(proxy) |
| 147 | host, selector = splithost(proxy) |
| 148 | url = (host, fullurl) # Signal special case to open_*() |
| 149 | name = 'open_' + type |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 150 | self.type = type |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 151 | if '-' in name: |
| 152 | # replace - with _ |
| 153 | name = string.join(string.split(name, '-'), '_') |
| 154 | if not hasattr(self, name): |
| 155 | if data is None: |
| 156 | return self.open_unknown(fullurl) |
| 157 | else: |
| 158 | return self.open_unknown(fullurl, data) |
| 159 | try: |
| 160 | if data is None: |
| 161 | return getattr(self, name)(url) |
| 162 | else: |
| 163 | return getattr(self, name)(url, data) |
| 164 | except socket.error, msg: |
| 165 | raise IOError, ('socket error', msg), sys.exc_info()[2] |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 166 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 167 | def open_unknown(self, fullurl, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 168 | """Overridable interface to open unknown URL type.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 169 | type, url = splittype(fullurl) |
| 170 | raise IOError, ('url error', 'unknown url type', type) |
Guido van Rossum | ca44540 | 1995-08-29 19:19:12 +0000 | [diff] [blame] | 171 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 172 | # External interface |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 173 | def retrieve(self, url, filename=None, reporthook=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 174 | """retrieve(url) returns (filename, None) for a local object |
| 175 | or (tempfilename, headers) for a remote object.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 176 | url = unwrap(url) |
| 177 | if self.tempcache and self.tempcache.has_key(url): |
| 178 | return self.tempcache[url] |
| 179 | type, url1 = splittype(url) |
| 180 | if not filename and (not type or type == 'file'): |
| 181 | try: |
| 182 | fp = self.open_local_file(url1) |
| 183 | hdrs = fp.info() |
| 184 | del fp |
| 185 | return url2pathname(splithost(url1)[1]), hdrs |
| 186 | except IOError, msg: |
| 187 | pass |
| 188 | fp = self.open(url) |
| 189 | headers = fp.info() |
| 190 | if not filename: |
| 191 | import tempfile |
| 192 | garbage, path = splittype(url) |
| 193 | garbage, path = splithost(path or "") |
| 194 | path, garbage = splitquery(path or "") |
| 195 | path, garbage = splitattr(path or "") |
| 196 | suffix = os.path.splitext(path)[1] |
| 197 | filename = tempfile.mktemp(suffix) |
| 198 | self.__tempfiles.append(filename) |
| 199 | result = filename, headers |
| 200 | if self.tempcache is not None: |
| 201 | self.tempcache[url] = result |
| 202 | tfp = open(filename, 'wb') |
| 203 | bs = 1024*8 |
| 204 | size = -1 |
| 205 | blocknum = 1 |
| 206 | if reporthook: |
| 207 | if headers.has_key("content-length"): |
| 208 | size = int(headers["Content-Length"]) |
| 209 | reporthook(0, bs, size) |
| 210 | block = fp.read(bs) |
| 211 | if reporthook: |
| 212 | reporthook(1, bs, size) |
| 213 | while block: |
| 214 | tfp.write(block) |
| 215 | block = fp.read(bs) |
| 216 | blocknum = blocknum + 1 |
| 217 | if reporthook: |
| 218 | reporthook(blocknum, bs, size) |
| 219 | fp.close() |
| 220 | tfp.close() |
| 221 | del fp |
| 222 | del tfp |
| 223 | return result |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 224 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 225 | # 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] | 226 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 227 | def open_http(self, url, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 228 | """Use HTTP protocol.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 229 | import httplib |
| 230 | user_passwd = None |
| 231 | if type(url) is type(""): |
| 232 | host, selector = splithost(url) |
| 233 | if host: |
| 234 | user_passwd, host = splituser(host) |
| 235 | host = unquote(host) |
| 236 | realhost = host |
| 237 | else: |
| 238 | host, selector = url |
| 239 | urltype, rest = splittype(selector) |
| 240 | url = rest |
| 241 | user_passwd = None |
| 242 | if string.lower(urltype) != 'http': |
| 243 | realhost = None |
| 244 | else: |
| 245 | realhost, rest = splithost(rest) |
| 246 | if realhost: |
| 247 | user_passwd, realhost = splituser(realhost) |
| 248 | if user_passwd: |
| 249 | selector = "%s://%s%s" % (urltype, realhost, rest) |
| 250 | #print "proxy via http:", host, selector |
| 251 | if not host: raise IOError, ('http error', 'no host given') |
| 252 | if user_passwd: |
| 253 | import base64 |
| 254 | auth = string.strip(base64.encodestring(user_passwd)) |
| 255 | else: |
| 256 | auth = None |
| 257 | h = httplib.HTTP(host) |
| 258 | if data is not None: |
| 259 | h.putrequest('POST', selector) |
| 260 | h.putheader('Content-type', 'application/x-www-form-urlencoded') |
| 261 | h.putheader('Content-length', '%d' % len(data)) |
| 262 | else: |
| 263 | h.putrequest('GET', selector) |
| 264 | if auth: h.putheader('Authorization', 'Basic %s' % auth) |
| 265 | if realhost: h.putheader('Host', realhost) |
| 266 | for args in self.addheaders: apply(h.putheader, args) |
| 267 | h.endheaders() |
| 268 | if data is not None: |
| 269 | h.send(data + '\r\n') |
| 270 | errcode, errmsg, headers = h.getreply() |
| 271 | fp = h.getfile() |
| 272 | if errcode == 200: |
| 273 | return addinfourl(fp, headers, "http:" + url) |
| 274 | else: |
| 275 | if data is None: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 276 | return self.http_error(url, fp, errcode, errmsg, headers) |
Guido van Rossum | 29aab75 | 1999-03-09 19:31:21 +0000 | [diff] [blame] | 277 | else: |
| 278 | return self.http_error(url, fp, errcode, errmsg, headers, data) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 279 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 280 | def http_error(self, url, fp, errcode, errmsg, headers, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 281 | """Handle http errors. |
| 282 | Derived class can override this, or provide specific handlers |
| 283 | named http_error_DDD where DDD is the 3-digit error code.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 284 | # First check if there's a specific handler for this error |
| 285 | name = 'http_error_%d' % errcode |
| 286 | if hasattr(self, name): |
| 287 | method = getattr(self, name) |
| 288 | if data is None: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 289 | result = method(url, fp, errcode, errmsg, headers) |
Jeremy Hylton | b30f52a | 1999-02-25 16:14:58 +0000 | [diff] [blame] | 290 | else: |
| 291 | result = method(url, fp, errcode, errmsg, headers, data) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 292 | if result: return result |
Jeremy Hylton | b30f52a | 1999-02-25 16:14:58 +0000 | [diff] [blame] | 293 | return self.http_error_default(url, fp, errcode, errmsg, headers) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 294 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 295 | def http_error_default(self, url, fp, errcode, errmsg, headers): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 296 | """Default error handler: close the connection and raise IOError.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 297 | void = fp.read() |
| 298 | fp.close() |
| 299 | raise IOError, ('http error', errcode, errmsg, headers) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 301 | if hasattr(socket, "ssl"): |
Andrew M. Kuchling | 141e989 | 2000-04-23 02:53:11 +0000 | [diff] [blame] | 302 | def open_https(self, url, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 303 | """Use HTTPS protocol.""" |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 304 | import httplib |
| 305 | if type(url) is type(""): |
| 306 | host, selector = splithost(url) |
| 307 | user_passwd, host = splituser(host) |
| 308 | else: |
| 309 | host, selector = url |
| 310 | urltype, rest = splittype(selector) |
| 311 | if string.lower(urltype) == 'https': |
| 312 | realhost, rest = splithost(rest) |
| 313 | user_passwd, realhost = splituser(realhost) |
| 314 | if user_passwd: |
| 315 | selector = "%s://%s%s" % (urltype, realhost, rest) |
Andrew M. Kuchling | 7ad4792 | 2000-06-10 01:41:48 +0000 | [diff] [blame] | 316 | #print "proxy via https:", host, selector |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 317 | if not host: raise IOError, ('https error', 'no host given') |
| 318 | if user_passwd: |
| 319 | import base64 |
| 320 | auth = string.strip(base64.encodestring(user_passwd)) |
| 321 | else: |
| 322 | auth = None |
| 323 | h = httplib.HTTPS(host, 0, |
| 324 | key_file=self.key_file, |
| 325 | cert_file=self.cert_file) |
Andrew M. Kuchling | 141e989 | 2000-04-23 02:53:11 +0000 | [diff] [blame] | 326 | if data is not None: |
| 327 | h.putrequest('POST', selector) |
| 328 | h.putheader('Content-type', |
| 329 | 'application/x-www-form-urlencoded') |
| 330 | h.putheader('Content-length', '%d' % len(data)) |
| 331 | else: |
| 332 | h.putrequest('GET', selector) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 333 | if auth: h.putheader('Authorization: Basic %s' % auth) |
| 334 | for args in self.addheaders: apply(h.putheader, args) |
| 335 | h.endheaders() |
Andrew M. Kuchling | 43c5af0 | 2000-04-24 14:17:06 +0000 | [diff] [blame] | 336 | if data is not None: |
| 337 | h.send(data + '\r\n') |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 338 | errcode, errmsg, headers = h.getreply() |
| 339 | fp = h.getfile() |
| 340 | if errcode == 200: |
| 341 | return addinfourl(fp, headers, url) |
| 342 | else: |
| 343 | return self.http_error(url, fp, errcode, errmsg, headers) |
| 344 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 345 | def open_gopher(self, url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 346 | """Use Gopher protocol.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 347 | import gopherlib |
| 348 | host, selector = splithost(url) |
| 349 | if not host: raise IOError, ('gopher error', 'no host given') |
| 350 | host = unquote(host) |
| 351 | type, selector = splitgophertype(selector) |
| 352 | selector, query = splitquery(selector) |
| 353 | selector = unquote(selector) |
| 354 | if query: |
| 355 | query = unquote(query) |
| 356 | fp = gopherlib.send_query(selector, query, host) |
| 357 | else: |
| 358 | fp = gopherlib.send_selector(selector, host) |
| 359 | return addinfourl(fp, noheaders(), "gopher:" + url) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 360 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 361 | def open_file(self, url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 362 | """Use local file or FTP depending on form of URL.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 363 | if url[:2] == '//' and url[2:3] != '/': |
| 364 | return self.open_ftp(url) |
| 365 | else: |
| 366 | return self.open_local_file(url) |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 367 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 368 | def open_local_file(self, url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 369 | """Use local file.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 370 | import mimetypes, mimetools, StringIO |
| 371 | mtype = mimetypes.guess_type(url)[0] |
| 372 | headers = mimetools.Message(StringIO.StringIO( |
| 373 | 'Content-Type: %s\n' % (mtype or 'text/plain'))) |
| 374 | host, file = splithost(url) |
| 375 | if not host: |
Guido van Rossum | 336a201 | 1999-06-24 15:27:36 +0000 | [diff] [blame] | 376 | urlfile = file |
| 377 | if file[:1] == '/': |
| 378 | urlfile = 'file://' + file |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 379 | return addinfourl(open(url2pathname(file), 'rb'), |
Guido van Rossum | 336a201 | 1999-06-24 15:27:36 +0000 | [diff] [blame] | 380 | headers, urlfile) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 381 | host, port = splitport(host) |
| 382 | if not port \ |
| 383 | and socket.gethostbyname(host) in (localhost(), thishost()): |
Guido van Rossum | 336a201 | 1999-06-24 15:27:36 +0000 | [diff] [blame] | 384 | urlfile = file |
| 385 | if file[:1] == '/': |
| 386 | urlfile = 'file://' + file |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 387 | return addinfourl(open(url2pathname(file), 'rb'), |
Guido van Rossum | 336a201 | 1999-06-24 15:27:36 +0000 | [diff] [blame] | 388 | headers, urlfile) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 389 | raise IOError, ('local file error', 'not on local host') |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 390 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 391 | def open_ftp(self, url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 392 | """Use FTP protocol.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 393 | host, path = splithost(url) |
| 394 | if not host: raise IOError, ('ftp error', 'no host given') |
| 395 | host, port = splitport(host) |
| 396 | user, host = splituser(host) |
| 397 | if user: user, passwd = splitpasswd(user) |
| 398 | else: passwd = None |
| 399 | host = unquote(host) |
| 400 | user = unquote(user or '') |
| 401 | passwd = unquote(passwd or '') |
| 402 | host = socket.gethostbyname(host) |
| 403 | if not port: |
| 404 | import ftplib |
| 405 | port = ftplib.FTP_PORT |
| 406 | else: |
| 407 | port = int(port) |
| 408 | path, attrs = splitattr(path) |
| 409 | path = unquote(path) |
| 410 | dirs = string.splitfields(path, '/') |
| 411 | dirs, file = dirs[:-1], dirs[-1] |
| 412 | if dirs and not dirs[0]: dirs = dirs[1:] |
Guido van Rossum | 5e006a3 | 1999-08-18 17:40:33 +0000 | [diff] [blame] | 413 | if dirs and not dirs[0]: dirs[0] = '/' |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 414 | key = (user, host, port, string.joinfields(dirs, '/')) |
| 415 | # XXX thread unsafe! |
| 416 | if len(self.ftpcache) > MAXFTPCACHE: |
| 417 | # Prune the cache, rather arbitrarily |
| 418 | for k in self.ftpcache.keys(): |
| 419 | if k != key: |
| 420 | v = self.ftpcache[k] |
| 421 | del self.ftpcache[k] |
| 422 | v.close() |
| 423 | try: |
| 424 | if not self.ftpcache.has_key(key): |
| 425 | self.ftpcache[key] = \ |
| 426 | ftpwrapper(user, passwd, host, port, dirs) |
| 427 | if not file: type = 'D' |
| 428 | else: type = 'I' |
| 429 | for attr in attrs: |
| 430 | attr, value = splitvalue(attr) |
| 431 | if string.lower(attr) == 'type' and \ |
| 432 | value in ('a', 'A', 'i', 'I', 'd', 'D'): |
| 433 | type = string.upper(value) |
| 434 | (fp, retrlen) = self.ftpcache[key].retrfile(file, type) |
| 435 | if retrlen is not None and retrlen >= 0: |
| 436 | import mimetools, StringIO |
| 437 | headers = mimetools.Message(StringIO.StringIO( |
| 438 | 'Content-Length: %d\n' % retrlen)) |
| 439 | else: |
| 440 | headers = noheaders() |
| 441 | return addinfourl(fp, headers, "ftp:" + url) |
| 442 | except ftperrors(), msg: |
| 443 | raise IOError, ('ftp error', msg), sys.exc_info()[2] |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 444 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 445 | def open_data(self, url, data=None): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 446 | """Use "data" URL.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 447 | # ignore POSTed data |
| 448 | # |
| 449 | # syntax of data URLs: |
| 450 | # dataurl := "data:" [ mediatype ] [ ";base64" ] "," data |
| 451 | # mediatype := [ type "/" subtype ] *( ";" parameter ) |
| 452 | # data := *urlchar |
| 453 | # parameter := attribute "=" value |
| 454 | import StringIO, mimetools, time |
| 455 | try: |
| 456 | [type, data] = string.split(url, ',', 1) |
| 457 | except ValueError: |
| 458 | raise IOError, ('data error', 'bad data URL') |
| 459 | if not type: |
| 460 | type = 'text/plain;charset=US-ASCII' |
| 461 | semi = string.rfind(type, ';') |
| 462 | if semi >= 0 and '=' not in type[semi:]: |
| 463 | encoding = type[semi+1:] |
| 464 | type = type[:semi] |
| 465 | else: |
| 466 | encoding = '' |
| 467 | msg = [] |
| 468 | msg.append('Date: %s'%time.strftime('%a, %d %b %Y %T GMT', |
| 469 | time.gmtime(time.time()))) |
| 470 | msg.append('Content-type: %s' % type) |
| 471 | if encoding == 'base64': |
| 472 | import base64 |
| 473 | data = base64.decodestring(data) |
| 474 | else: |
| 475 | data = unquote(data) |
| 476 | msg.append('Content-length: %d' % len(data)) |
| 477 | msg.append('') |
| 478 | msg.append(data) |
| 479 | msg = string.join(msg, '\n') |
| 480 | f = StringIO.StringIO(msg) |
| 481 | headers = mimetools.Message(f, 0) |
| 482 | f.fileno = None # needed for addinfourl |
| 483 | return addinfourl(f, headers, url) |
Guido van Rossum | 6d4d1c2 | 1998-03-12 14:32:55 +0000 | [diff] [blame] | 484 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 486 | class FancyURLopener(URLopener): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 487 | """Derived class with handlers for errors we can handle (perhaps).""" |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 488 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 489 | def __init__(self, *args): |
| 490 | apply(URLopener.__init__, (self,) + args) |
| 491 | self.auth_cache = {} |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 492 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 493 | def http_error_default(self, url, fp, errcode, errmsg, headers): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 494 | """Default error handling -- don't raise an exception.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 495 | return addinfourl(fp, headers, "http:" + url) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 496 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 497 | def http_error_302(self, url, fp, errcode, errmsg, headers, data=None): |
| 498 | """Error 302 -- relocated (temporarily).""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 499 | # XXX The server can force infinite recursion here! |
| 500 | if headers.has_key('location'): |
| 501 | newurl = headers['location'] |
| 502 | elif headers.has_key('uri'): |
| 503 | newurl = headers['uri'] |
| 504 | else: |
| 505 | return |
| 506 | void = fp.read() |
| 507 | fp.close() |
Guido van Rossum | 3527f59 | 1999-03-29 20:23:41 +0000 | [diff] [blame] | 508 | # In case the server sent a relative URL, join with original: |
| 509 | newurl = basejoin("http:" + url, newurl) |
Guido van Rossum | 3c8baed | 2000-02-01 23:36:55 +0000 | [diff] [blame] | 510 | if data is None: |
| 511 | return self.open(newurl) |
| 512 | else: |
| 513 | return self.open(newurl, data) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 514 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 515 | def http_error_301(self, url, fp, errcode, errmsg, headers, data=None): |
| 516 | """Error 301 -- also relocated (permanently).""" |
| 517 | return self.http_error_302(url, fp, errcode, errmsg, headers, data) |
Guido van Rossum | e6ad891 | 1996-09-10 17:02:56 +0000 | [diff] [blame] | 518 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 519 | def http_error_401(self, url, fp, errcode, errmsg, headers, data=None): |
| 520 | """Error 401 -- authentication required. |
| 521 | See this URL for a description of the basic authentication scheme: |
| 522 | http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 523 | if headers.has_key('www-authenticate'): |
| 524 | stuff = headers['www-authenticate'] |
| 525 | import re |
| 526 | match = re.match('[ \t]*([^ \t]+)[ \t]+realm="([^"]*)"', stuff) |
| 527 | if match: |
| 528 | scheme, realm = match.groups() |
| 529 | if string.lower(scheme) == 'basic': |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 530 | name = 'retry_' + self.type + '_basic_auth' |
Guido van Rossum | 3c8baed | 2000-02-01 23:36:55 +0000 | [diff] [blame] | 531 | if data is None: |
| 532 | return getattr(self,name)(url, realm) |
| 533 | else: |
| 534 | return getattr(self,name)(url, realm, data) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 535 | |
Guido van Rossum | 3c8baed | 2000-02-01 23:36:55 +0000 | [diff] [blame] | 536 | def retry_http_basic_auth(self, url, realm, data=None): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 537 | host, selector = splithost(url) |
| 538 | i = string.find(host, '@') + 1 |
| 539 | host = host[i:] |
| 540 | user, passwd = self.get_user_passwd(host, realm, i) |
| 541 | if not (user or passwd): return None |
| 542 | host = user + ':' + passwd + '@' + host |
| 543 | newurl = 'http://' + host + selector |
Guido van Rossum | 3c8baed | 2000-02-01 23:36:55 +0000 | [diff] [blame] | 544 | if data is None: |
| 545 | return self.open(newurl) |
| 546 | else: |
| 547 | return self.open(newurl, data) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 548 | |
Guido van Rossum | 3c8baed | 2000-02-01 23:36:55 +0000 | [diff] [blame] | 549 | def retry_https_basic_auth(self, url, realm, data=None): |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 550 | host, selector = splithost(url) |
| 551 | i = string.find(host, '@') + 1 |
| 552 | host = host[i:] |
| 553 | user, passwd = self.get_user_passwd(host, realm, i) |
| 554 | if not (user or passwd): return None |
| 555 | host = user + ':' + passwd + '@' + host |
| 556 | newurl = '//' + host + selector |
| 557 | return self.open_https(newurl) |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 558 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 559 | def get_user_passwd(self, host, realm, clear_cache = 0): |
| 560 | key = realm + '@' + string.lower(host) |
| 561 | if self.auth_cache.has_key(key): |
| 562 | if clear_cache: |
| 563 | del self.auth_cache[key] |
| 564 | else: |
| 565 | return self.auth_cache[key] |
| 566 | user, passwd = self.prompt_user_passwd(host, realm) |
| 567 | if user or passwd: self.auth_cache[key] = (user, passwd) |
| 568 | return user, passwd |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 569 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 570 | def prompt_user_passwd(self, host, realm): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 571 | """Override this in a GUI environment!""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 572 | import getpass |
| 573 | try: |
| 574 | user = raw_input("Enter username for %s at %s: " % (realm, |
| 575 | host)) |
| 576 | passwd = getpass.getpass("Enter password for %s in %s at %s: " % |
| 577 | (user, realm, host)) |
| 578 | return user, passwd |
| 579 | except KeyboardInterrupt: |
| 580 | print |
| 581 | return None, None |
Guido van Rossum | bbb0a05 | 1995-08-04 04:29:05 +0000 | [diff] [blame] | 582 | |
| 583 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 584 | # Utility functions |
| 585 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 586 | _localhost = None |
| 587 | def localhost(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 588 | """Return the IP address of the magic hostname 'localhost'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 589 | global _localhost |
| 590 | if not _localhost: |
| 591 | _localhost = socket.gethostbyname('localhost') |
| 592 | return _localhost |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 593 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 594 | _thishost = None |
| 595 | def thishost(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 596 | """Return the IP address of the current host.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 597 | global _thishost |
| 598 | if not _thishost: |
| 599 | _thishost = socket.gethostbyname(socket.gethostname()) |
| 600 | return _thishost |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 601 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 602 | _ftperrors = None |
| 603 | def ftperrors(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 604 | """Return the set of errors raised by the FTP class.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 605 | global _ftperrors |
| 606 | if not _ftperrors: |
| 607 | import ftplib |
| 608 | _ftperrors = ftplib.all_errors |
| 609 | return _ftperrors |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 610 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 611 | _noheaders = None |
| 612 | def noheaders(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 613 | """Return an empty mimetools.Message object.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 614 | global _noheaders |
| 615 | if not _noheaders: |
| 616 | import mimetools |
| 617 | import StringIO |
| 618 | _noheaders = mimetools.Message(StringIO.StringIO(), 0) |
| 619 | _noheaders.fp.close() # Recycle file descriptor |
| 620 | return _noheaders |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 621 | |
| 622 | |
| 623 | # Utility classes |
| 624 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 625 | class ftpwrapper: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 626 | """Class used by open_ftp() for cache of open FTP connections.""" |
| 627 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 628 | def __init__(self, user, passwd, host, port, dirs): |
| 629 | self.user = user |
| 630 | self.passwd = passwd |
| 631 | self.host = host |
| 632 | self.port = port |
| 633 | self.dirs = dirs |
| 634 | self.init() |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 635 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 636 | def init(self): |
| 637 | import ftplib |
| 638 | self.busy = 0 |
| 639 | self.ftp = ftplib.FTP() |
| 640 | self.ftp.connect(self.host, self.port) |
| 641 | self.ftp.login(self.user, self.passwd) |
| 642 | for dir in self.dirs: |
| 643 | self.ftp.cwd(dir) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 644 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 645 | def retrfile(self, file, type): |
| 646 | import ftplib |
| 647 | self.endtransfer() |
| 648 | if type in ('d', 'D'): cmd = 'TYPE A'; isdir = 1 |
| 649 | else: cmd = 'TYPE ' + type; isdir = 0 |
| 650 | try: |
| 651 | self.ftp.voidcmd(cmd) |
| 652 | except ftplib.all_errors: |
| 653 | self.init() |
| 654 | self.ftp.voidcmd(cmd) |
| 655 | conn = None |
| 656 | if file and not isdir: |
| 657 | # Use nlst to see if the file exists at all |
| 658 | try: |
| 659 | self.ftp.nlst(file) |
| 660 | except ftplib.error_perm, reason: |
| 661 | raise IOError, ('ftp error', reason), sys.exc_info()[2] |
| 662 | # Restore the transfer mode! |
| 663 | self.ftp.voidcmd(cmd) |
| 664 | # Try to retrieve as a file |
| 665 | try: |
| 666 | cmd = 'RETR ' + file |
| 667 | conn = self.ftp.ntransfercmd(cmd) |
| 668 | except ftplib.error_perm, reason: |
| 669 | if reason[:3] != '550': |
| 670 | raise IOError, ('ftp error', reason), sys.exc_info()[2] |
| 671 | if not conn: |
| 672 | # Set transfer mode to ASCII! |
| 673 | self.ftp.voidcmd('TYPE A') |
| 674 | # Try a directory listing |
| 675 | if file: cmd = 'LIST ' + file |
| 676 | else: cmd = 'LIST' |
| 677 | conn = self.ftp.ntransfercmd(cmd) |
| 678 | self.busy = 1 |
| 679 | # Pass back both a suitably decorated object and a retrieval length |
| 680 | return (addclosehook(conn[0].makefile('rb'), |
| 681 | self.endtransfer), conn[1]) |
| 682 | def endtransfer(self): |
| 683 | if not self.busy: |
| 684 | return |
| 685 | self.busy = 0 |
| 686 | try: |
| 687 | self.ftp.voidresp() |
| 688 | except ftperrors(): |
| 689 | pass |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 690 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 691 | def close(self): |
| 692 | self.endtransfer() |
| 693 | try: |
| 694 | self.ftp.close() |
| 695 | except ftperrors(): |
| 696 | pass |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 697 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 698 | class addbase: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 699 | """Base class for addinfo and addclosehook.""" |
| 700 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 701 | def __init__(self, fp): |
| 702 | self.fp = fp |
| 703 | self.read = self.fp.read |
| 704 | self.readline = self.fp.readline |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 705 | if hasattr(self.fp, "readlines"): self.readlines = self.fp.readlines |
| 706 | if hasattr(self.fp, "fileno"): self.fileno = self.fp.fileno |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 707 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 708 | def __repr__(self): |
| 709 | return '<%s at %s whose fp = %s>' % (self.__class__.__name__, |
| 710 | `id(self)`, `self.fp`) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 711 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 712 | def close(self): |
| 713 | self.read = None |
| 714 | self.readline = None |
| 715 | self.readlines = None |
| 716 | self.fileno = None |
| 717 | if self.fp: self.fp.close() |
| 718 | self.fp = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 719 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 720 | class addclosehook(addbase): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 721 | """Class to add a close hook to an open file.""" |
| 722 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 723 | def __init__(self, fp, closehook, *hookargs): |
| 724 | addbase.__init__(self, fp) |
| 725 | self.closehook = closehook |
| 726 | self.hookargs = hookargs |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 727 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 728 | def close(self): |
Guido van Rossum | c580dae | 2000-05-24 13:21:46 +0000 | [diff] [blame] | 729 | addbase.close(self) |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 730 | if self.closehook: |
| 731 | apply(self.closehook, self.hookargs) |
| 732 | self.closehook = None |
| 733 | self.hookargs = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 734 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 735 | class addinfo(addbase): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 736 | """class to add an info() method to an open file.""" |
| 737 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 738 | def __init__(self, fp, headers): |
| 739 | addbase.__init__(self, fp) |
| 740 | self.headers = headers |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 741 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 742 | def info(self): |
| 743 | return self.headers |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 744 | |
Guido van Rossum | e6ad891 | 1996-09-10 17:02:56 +0000 | [diff] [blame] | 745 | class addinfourl(addbase): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 746 | """class to add info() and geturl() methods to an open file.""" |
| 747 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 748 | def __init__(self, fp, headers, url): |
| 749 | addbase.__init__(self, fp) |
| 750 | self.headers = headers |
| 751 | self.url = url |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 752 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 753 | def info(self): |
| 754 | return self.headers |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 755 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 756 | def geturl(self): |
| 757 | return self.url |
Guido van Rossum | e6ad891 | 1996-09-10 17:02:56 +0000 | [diff] [blame] | 758 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 759 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 760 | def basejoin(base, url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 761 | """Utility to combine a URL with a base URL to form a new URL.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 762 | type, path = splittype(url) |
| 763 | if type: |
| 764 | # if url is complete (i.e., it contains a type), return it |
| 765 | return url |
| 766 | host, path = splithost(path) |
| 767 | type, basepath = splittype(base) # inherit type from base |
| 768 | if host: |
| 769 | # if url contains host, just inherit type |
| 770 | if type: return type + '://' + host + path |
| 771 | else: |
| 772 | # no type inherited, so url must have started with // |
| 773 | # just return it |
| 774 | return url |
| 775 | host, basepath = splithost(basepath) # inherit host |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 776 | basepath, basetag = splittag(basepath) # remove extraneous cruft |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 777 | basepath, basequery = splitquery(basepath) # idem |
| 778 | if path[:1] != '/': |
| 779 | # non-absolute path name |
| 780 | if path[:1] in ('#', '?'): |
| 781 | # path is just a tag or query, attach to basepath |
| 782 | i = len(basepath) |
| 783 | else: |
| 784 | # else replace last component |
| 785 | i = string.rfind(basepath, '/') |
| 786 | if i < 0: |
| 787 | # basepath not absolute |
| 788 | if host: |
| 789 | # host present, make absolute |
| 790 | basepath = '/' |
| 791 | else: |
| 792 | # else keep non-absolute |
| 793 | basepath = '' |
| 794 | else: |
| 795 | # remove last file component |
| 796 | basepath = basepath[:i+1] |
| 797 | # Interpret ../ (important because of symlinks) |
| 798 | while basepath and path[:3] == '../': |
| 799 | path = path[3:] |
| 800 | i = string.rfind(basepath[:-1], '/') |
| 801 | if i > 0: |
| 802 | basepath = basepath[:i+1] |
| 803 | elif i == 0: |
| 804 | basepath = '/' |
| 805 | break |
| 806 | else: |
| 807 | basepath = '' |
| 808 | |
| 809 | path = basepath + path |
| 810 | if type and host: return type + '://' + host + path |
| 811 | elif type: return type + ':' + path |
| 812 | elif host: return '//' + host + path # don't know what this means |
| 813 | else: return path |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 814 | |
| 815 | |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 816 | # Utilities to parse URLs (most of these return None for missing parts): |
Sjoerd Mullender | e0371b8 | 1995-11-10 10:36:07 +0000 | [diff] [blame] | 817 | # unwrap('<URL:type://host/path>') --> 'type://host/path' |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 818 | # splittype('type:opaquestring') --> 'type', 'opaquestring' |
| 819 | # splithost('//host[:port]/path') --> 'host[:port]', '/path' |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 820 | # splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]' |
| 821 | # splitpasswd('user:passwd') -> 'user', 'passwd' |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 822 | # splitport('host:port') --> 'host', 'port' |
| 823 | # splitquery('/path?query') --> '/path', 'query' |
| 824 | # splittag('/path#tag') --> '/path', 'tag' |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 825 | # splitattr('/path;attr1=value1;attr2=value2;...') -> |
| 826 | # '/path', ['attr1=value1', 'attr2=value2', ...] |
| 827 | # splitvalue('attr=value') --> 'attr', 'value' |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 828 | # splitgophertype('/Xselector') --> 'X', 'selector' |
| 829 | # unquote('abc%20def') -> 'abc def' |
| 830 | # quote('abc def') -> 'abc%20def') |
| 831 | |
| 832 | def unwrap(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 833 | """unwrap('<URL:type://host/path>') --> 'type://host/path'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 834 | url = string.strip(url) |
| 835 | if url[:1] == '<' and url[-1:] == '>': |
| 836 | url = string.strip(url[1:-1]) |
| 837 | if url[:4] == 'URL:': url = string.strip(url[4:]) |
| 838 | return url |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 839 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 840 | _typeprog = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 841 | def splittype(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 842 | """splittype('type:opaquestring') --> 'type', 'opaquestring'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 843 | global _typeprog |
| 844 | if _typeprog is None: |
| 845 | import re |
| 846 | _typeprog = re.compile('^([^/:]+):') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 847 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 848 | match = _typeprog.match(url) |
| 849 | if match: |
| 850 | scheme = match.group(1) |
Fred Drake | 9e94afd | 2000-07-01 07:03:30 +0000 | [diff] [blame] | 851 | return scheme.lower(), url[len(scheme) + 1:] |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 852 | return None, url |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 853 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 854 | _hostprog = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 855 | def splithost(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 856 | """splithost('//host[:port]/path') --> 'host[:port]', '/path'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 857 | global _hostprog |
| 858 | if _hostprog is None: |
| 859 | import re |
Guido van Rossum | 3427c1f | 1999-07-01 23:20:56 +0000 | [diff] [blame] | 860 | _hostprog = re.compile('^//([^/]*)(.*)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 861 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 862 | match = _hostprog.match(url) |
| 863 | if match: return match.group(1, 2) |
| 864 | return None, url |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 865 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 866 | _userprog = None |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 867 | def splituser(host): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 868 | """splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 869 | global _userprog |
| 870 | if _userprog is None: |
| 871 | import re |
| 872 | _userprog = re.compile('^([^@]*)@(.*)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 873 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 874 | match = _userprog.match(host) |
| 875 | if match: return match.group(1, 2) |
| 876 | return None, host |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 877 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 878 | _passwdprog = None |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 879 | def splitpasswd(user): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 880 | """splitpasswd('user:passwd') -> 'user', 'passwd'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 881 | global _passwdprog |
| 882 | if _passwdprog is None: |
| 883 | import re |
| 884 | _passwdprog = re.compile('^([^:]*):(.*)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 885 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 886 | match = _passwdprog.match(user) |
| 887 | if match: return match.group(1, 2) |
| 888 | return user, None |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 889 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 890 | # splittag('/path#tag') --> '/path', 'tag' |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 891 | _portprog = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 892 | def splitport(host): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 893 | """splitport('host:port') --> 'host', 'port'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 894 | global _portprog |
| 895 | if _portprog is None: |
| 896 | import re |
| 897 | _portprog = re.compile('^(.*):([0-9]+)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 898 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 899 | match = _portprog.match(host) |
| 900 | if match: return match.group(1, 2) |
| 901 | return host, None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 902 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 903 | _nportprog = None |
Guido van Rossum | 53725a2 | 1996-06-13 19:12:35 +0000 | [diff] [blame] | 904 | def splitnport(host, defport=-1): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 905 | """Split host and port, returning numeric port. |
| 906 | Return given default port if no ':' found; defaults to -1. |
| 907 | Return numerical port if a valid number are found after ':'. |
| 908 | Return None if ':' but not a valid number.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 909 | global _nportprog |
| 910 | if _nportprog is None: |
| 911 | import re |
| 912 | _nportprog = re.compile('^(.*):(.*)$') |
Guido van Rossum | 7e7ca0b | 1998-03-26 21:01:39 +0000 | [diff] [blame] | 913 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 914 | match = _nportprog.match(host) |
| 915 | if match: |
| 916 | host, port = match.group(1, 2) |
| 917 | try: |
| 918 | if not port: raise string.atoi_error, "no digits" |
| 919 | nport = string.atoi(port) |
| 920 | except string.atoi_error: |
| 921 | nport = None |
| 922 | return host, nport |
| 923 | return host, defport |
Guido van Rossum | 53725a2 | 1996-06-13 19:12:35 +0000 | [diff] [blame] | 924 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 925 | _queryprog = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 926 | def splitquery(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 927 | """splitquery('/path?query') --> '/path', 'query'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 928 | global _queryprog |
| 929 | if _queryprog is None: |
| 930 | import re |
| 931 | _queryprog = re.compile('^(.*)\?([^?]*)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 932 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 933 | match = _queryprog.match(url) |
| 934 | if match: return match.group(1, 2) |
| 935 | return url, None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 936 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 937 | _tagprog = None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 938 | def splittag(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 939 | """splittag('/path#tag') --> '/path', 'tag'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 940 | global _tagprog |
| 941 | if _tagprog is None: |
| 942 | import re |
| 943 | _tagprog = re.compile('^(.*)#([^#]*)$') |
Guido van Rossum | 7e7ca0b | 1998-03-26 21:01:39 +0000 | [diff] [blame] | 944 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 945 | match = _tagprog.match(url) |
| 946 | if match: return match.group(1, 2) |
| 947 | return url, None |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 948 | |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 949 | def splitattr(url): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 950 | """splitattr('/path;attr1=value1;attr2=value2;...') -> |
| 951 | '/path', ['attr1=value1', 'attr2=value2', ...].""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 952 | words = string.splitfields(url, ';') |
| 953 | return words[0], words[1:] |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 954 | |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 955 | _valueprog = None |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 956 | def splitvalue(attr): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 957 | """splitvalue('attr=value') --> 'attr', 'value'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 958 | global _valueprog |
| 959 | if _valueprog is None: |
| 960 | import re |
| 961 | _valueprog = re.compile('^([^=]*)=(.*)$') |
Guido van Rossum | 332e144 | 1997-09-29 23:23:46 +0000 | [diff] [blame] | 962 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 963 | match = _valueprog.match(attr) |
| 964 | if match: return match.group(1, 2) |
| 965 | return attr, None |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 966 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 967 | def splitgophertype(selector): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 968 | """splitgophertype('/Xselector') --> 'X', 'selector'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 969 | if selector[:1] == '/' and selector[1:2]: |
| 970 | return selector[1], selector[2:] |
| 971 | return None, selector |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 972 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 973 | def unquote(s): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 974 | """unquote('abc%20def') -> 'abc def'.""" |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 975 | mychr = chr |
| 976 | myatoi = string.atoi |
| 977 | list = string.split(s, '%') |
| 978 | res = [list[0]] |
| 979 | myappend = res.append |
| 980 | del list[0] |
| 981 | for item in list: |
| 982 | if item[1:2]: |
| 983 | try: |
| 984 | myappend(mychr(myatoi(item[:2], 16)) |
| 985 | + item[2:]) |
| 986 | except: |
| 987 | myappend('%' + item) |
| 988 | else: |
| 989 | myappend('%' + item) |
| 990 | return string.join(res, "") |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 991 | |
Guido van Rossum | 0564e12 | 1996-12-13 14:47:36 +0000 | [diff] [blame] | 992 | def unquote_plus(s): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 993 | if '+' in s: |
| 994 | # replace '+' with ' ' |
| 995 | s = string.join(string.split(s, '+'), ' ') |
| 996 | return unquote(s) |
Guido van Rossum | 0564e12 | 1996-12-13 14:47:36 +0000 | [diff] [blame] | 997 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 998 | always_safe = string.letters + string.digits + '_,.-' |
Guido van Rossum | 7c395db | 1994-07-04 22:14:49 +0000 | [diff] [blame] | 999 | def quote(s, safe = '/'): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1000 | """quote('abc def') -> 'abc%20def').""" |
Guido van Rossum | 0dee4ee | 1999-06-09 15:14:50 +0000 | [diff] [blame] | 1001 | # XXX Can speed this up an order of magnitude |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1002 | safe = always_safe + safe |
| 1003 | res = list(s) |
| 1004 | for i in range(len(res)): |
| 1005 | c = res[i] |
| 1006 | if c not in safe: |
| 1007 | res[i] = '%%%02x' % ord(c) |
| 1008 | return string.joinfields(res, '') |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1009 | |
Guido van Rossum | 0564e12 | 1996-12-13 14:47:36 +0000 | [diff] [blame] | 1010 | def quote_plus(s, safe = '/'): |
Guido van Rossum | 0dee4ee | 1999-06-09 15:14:50 +0000 | [diff] [blame] | 1011 | # XXX Can speed this up an order of magnitude |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1012 | if ' ' in s: |
| 1013 | # replace ' ' with '+' |
| 1014 | l = string.split(s, ' ') |
| 1015 | for i in range(len(l)): |
| 1016 | l[i] = quote(l[i], safe) |
| 1017 | return string.join(l, '+') |
| 1018 | else: |
| 1019 | return quote(s, safe) |
Guido van Rossum | 0564e12 | 1996-12-13 14:47:36 +0000 | [diff] [blame] | 1020 | |
Guido van Rossum | 810a339 | 1998-07-22 21:33:23 +0000 | [diff] [blame] | 1021 | def urlencode(dict): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1022 | """Encode a dictionary of form entries into a URL query string.""" |
| 1023 | l = [] |
| 1024 | for k, v in dict.items(): |
| 1025 | k = quote_plus(str(k)) |
| 1026 | v = quote_plus(str(v)) |
| 1027 | l.append(k + '=' + v) |
| 1028 | return string.join(l, '&') |
Guido van Rossum | 810a339 | 1998-07-22 21:33:23 +0000 | [diff] [blame] | 1029 | |
Guido van Rossum | 442e720 | 1996-03-20 15:33:11 +0000 | [diff] [blame] | 1030 | |
| 1031 | # Proxy handling |
Guido van Rossum | 4163e70 | 1998-08-06 13:39:09 +0000 | [diff] [blame] | 1032 | if os.name == 'mac': |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1033 | def getproxies(): |
| 1034 | """Return a dictionary of scheme -> proxy server URL mappings. |
Guido van Rossum | 442e720 | 1996-03-20 15:33:11 +0000 | [diff] [blame] | 1035 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1036 | By convention the mac uses Internet Config to store |
| 1037 | proxies. An HTTP proxy, for instance, is stored under |
| 1038 | the HttpProxy key. |
Guido van Rossum | 442e720 | 1996-03-20 15:33:11 +0000 | [diff] [blame] | 1039 | |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1040 | """ |
| 1041 | try: |
| 1042 | import ic |
| 1043 | except ImportError: |
| 1044 | return {} |
| 1045 | |
| 1046 | try: |
| 1047 | config = ic.IC() |
| 1048 | except ic.error: |
| 1049 | return {} |
| 1050 | proxies = {} |
| 1051 | # HTTP: |
| 1052 | if config.has_key('UseHTTPProxy') and config['UseHTTPProxy']: |
| 1053 | try: |
| 1054 | value = config['HTTPProxyHost'] |
| 1055 | except ic.error: |
| 1056 | pass |
| 1057 | else: |
| 1058 | proxies['http'] = 'http://%s' % value |
| 1059 | # FTP: XXXX To be done. |
| 1060 | # Gopher: XXXX To be done. |
| 1061 | return proxies |
| 1062 | |
Guido van Rossum | 4163e70 | 1998-08-06 13:39:09 +0000 | [diff] [blame] | 1063 | else: |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1064 | def getproxies(): |
| 1065 | """Return a dictionary of scheme -> proxy server URL mappings. |
| 1066 | |
| 1067 | Scan the environment for variables named <scheme>_proxy; |
| 1068 | this seems to be the standard convention. If you need a |
| 1069 | different way, you can pass a proxies dictionary to the |
| 1070 | [Fancy]URLopener constructor. |
| 1071 | |
| 1072 | """ |
| 1073 | proxies = {} |
| 1074 | for name, value in os.environ.items(): |
| 1075 | name = string.lower(name) |
| 1076 | if value and name[-6:] == '_proxy': |
| 1077 | proxies[name[:-6]] = value |
| 1078 | return proxies |
Guido van Rossum | 442e720 | 1996-03-20 15:33:11 +0000 | [diff] [blame] | 1079 | |
| 1080 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1081 | # Test and time quote() and unquote() |
| 1082 | def test1(): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1083 | import time |
| 1084 | s = '' |
| 1085 | for i in range(256): s = s + chr(i) |
| 1086 | s = s*4 |
| 1087 | t0 = time.time() |
| 1088 | qs = quote(s) |
| 1089 | uqs = unquote(qs) |
| 1090 | t1 = time.time() |
| 1091 | if uqs != s: |
| 1092 | print 'Wrong!' |
| 1093 | print `s` |
| 1094 | print `qs` |
| 1095 | print `uqs` |
| 1096 | print round(t1 - t0, 3), 'sec' |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1097 | |
| 1098 | |
Guido van Rossum | 9ab96d4 | 1998-09-28 14:07:00 +0000 | [diff] [blame] | 1099 | def reporthook(blocknum, blocksize, totalsize): |
| 1100 | # Report during remote transfers |
| 1101 | print "Block number: %d, Block size: %d, Total size: %d" % (blocknum, blocksize, totalsize) |
| 1102 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1103 | # Test program |
Guido van Rossum | 2349015 | 1998-06-25 02:39:00 +0000 | [diff] [blame] | 1104 | def test(args=[]): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1105 | if not args: |
| 1106 | args = [ |
| 1107 | '/etc/passwd', |
| 1108 | 'file:/etc/passwd', |
| 1109 | 'file://localhost/etc/passwd', |
| 1110 | 'ftp://ftp.python.org/etc/passwd', |
| 1111 | ## 'gopher://gopher.micro.umn.edu/1/', |
| 1112 | 'http://www.python.org/index.html', |
| 1113 | ] |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 1114 | if hasattr(URLopener, "open_https"): |
| 1115 | args.append('https://synergy.as.cmu.edu/~geek/') |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1116 | try: |
| 1117 | for url in args: |
| 1118 | print '-'*10, url, '-'*10 |
| 1119 | fn, h = urlretrieve(url, None, reporthook) |
| 1120 | print fn, h |
| 1121 | if h: |
| 1122 | print '======' |
| 1123 | for k in h.keys(): print k + ':', h[k] |
| 1124 | print '======' |
| 1125 | fp = open(fn, 'rb') |
| 1126 | data = fp.read() |
| 1127 | del fp |
| 1128 | if '\r' in data: |
| 1129 | table = string.maketrans("", "") |
| 1130 | data = string.translate(data, table, "\r") |
| 1131 | print data |
| 1132 | fn, h = None, None |
| 1133 | print '-'*40 |
| 1134 | finally: |
| 1135 | urlcleanup() |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1136 | |
Guido van Rossum | 2349015 | 1998-06-25 02:39:00 +0000 | [diff] [blame] | 1137 | def main(): |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1138 | import getopt, sys |
| 1139 | try: |
| 1140 | opts, args = getopt.getopt(sys.argv[1:], "th") |
| 1141 | except getopt.error, msg: |
| 1142 | print msg |
| 1143 | print "Use -h for help" |
| 1144 | return |
| 1145 | t = 0 |
| 1146 | for o, a in opts: |
| 1147 | if o == '-t': |
| 1148 | t = t + 1 |
| 1149 | if o == '-h': |
| 1150 | print "Usage: python urllib.py [-t] [url ...]" |
| 1151 | print "-t runs self-test;", |
| 1152 | print "otherwise, contents of urls are printed" |
| 1153 | return |
| 1154 | if t: |
| 1155 | if t > 1: |
| 1156 | test1() |
| 1157 | test(args) |
| 1158 | else: |
| 1159 | if not args: |
| 1160 | print "Use -h for help" |
| 1161 | for url in args: |
| 1162 | print urlopen(url).read(), |
Guido van Rossum | 2349015 | 1998-06-25 02:39:00 +0000 | [diff] [blame] | 1163 | |
Guido van Rossum | 7c6ebb5 | 1994-03-22 12:05:32 +0000 | [diff] [blame] | 1164 | # Run test program when run as a script |
| 1165 | if __name__ == '__main__': |
Jeremy Hylton | f90b002 | 1999-02-25 16:12:12 +0000 | [diff] [blame] | 1166 | main() |