Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 1 | # Open an arbitrary URL |
| 2 | # |
| 3 | # See the following document for a tentative description of URLs: |
| 4 | # Uniform Resource Locators Tim Berners-Lee |
| 5 | # INTERNET DRAFT CERN |
| 6 | # IETF URL Working Group 14 July 1993 |
| 7 | # draft-ietf-uri-url-01.txt |
| 8 | # |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 9 | # The object returned by URLopener().open(file) will differ per |
| 10 | # protocol. All you know is that is has methods read(), readline(), |
| 11 | # readlines(), fileno(), close() and info(). The read*(), fileno() |
| 12 | # and close() methods work like those of open files. |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 13 | # The info() method returns an rfc822.Message object which can be |
| 14 | # used to query various info about the object, if available. |
| 15 | # (rfc822.Message objects are queried with the getheader() method.) |
| 16 | |
| 17 | import socket |
| 18 | import regex |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 19 | |
| 20 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 21 | # This really consists of two pieces: |
| 22 | # (1) a class which handles opening of all sorts of URLs |
| 23 | # (plus assorted utilities etc.) |
| 24 | # (2) a set of functions for parsing URLs |
| 25 | # XXX Should these be separated out into different modules? |
| 26 | |
| 27 | |
| 28 | # Shortcut for basic usage |
| 29 | _urlopener = None |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 30 | def urlopen(url): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 31 | global _urlopener |
| 32 | if not _urlopener: |
| 33 | _urlopener = URLopener() |
| 34 | return _urlopener.open(url) |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 35 | def urlretrieve(url): |
| 36 | global _urlopener |
| 37 | if not _urlopener: |
| 38 | _urlopener = URLopener() |
| 39 | return _urlopener.retrieve(url) |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame] | 40 | def urlcleanup(): |
| 41 | if _urlopener: |
| 42 | _urlopener.cleanup() |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 43 | |
| 44 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 45 | # Class to open URLs. |
| 46 | # This is a class rather than just a subroutine because we may need |
| 47 | # more than one set of global protocol-specific options. |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 48 | ftpcache = {} |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 49 | class URLopener: |
| 50 | |
| 51 | # Constructor |
| 52 | def __init__(self): |
| 53 | self.addheaders = [] |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame] | 54 | self.tempcache = {} |
Guido van Rossum | 914973a | 1994-02-24 15:55:43 +0000 | [diff] [blame] | 55 | self.ftpcache = ftpcache |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 56 | # Undocumented feature: you can use a different |
| 57 | # ftp cache by assigning to the .ftpcache member; |
| 58 | # in case you want logically independent URL openers |
| 59 | |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 60 | def __del__(self): |
| 61 | self.close() |
| 62 | |
| 63 | def close(self): |
| 64 | self.cleanup() |
| 65 | |
| 66 | def cleanup(self): |
| 67 | import os |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame] | 68 | for url in self.tempcache.keys(): |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 69 | try: |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame] | 70 | os.unlink(self.tempcache[url][0]) |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 71 | except os.error: |
| 72 | pass |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame] | 73 | del self.tempcache[url] |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 75 | # Add a header to be used by the HTTP interface only |
| 76 | # e.g. u.addheader('Accept', 'sound/basic') |
| 77 | def addheader(self, *args): |
| 78 | self.addheaders.append(args) |
| 79 | |
| 80 | # External interface |
| 81 | # Use URLopener().open(file) instead of open(file, 'r') |
| 82 | def open(self, url): |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 83 | type, url = splittype(unwrap(url)) |
| 84 | if not type: type = 'file' |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 85 | name = 'open_' + type |
| 86 | if '-' in name: |
| 87 | import regsub |
| 88 | name = regsub.gsub('-', '_', name) |
| 89 | if not hasattr(self, name): |
| 90 | raise IOError, ('url error', 'unknown url type', type) |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 91 | try: |
Guido van Rossum | 914973a | 1994-02-24 15:55:43 +0000 | [diff] [blame] | 92 | return getattr(self, name)(url) |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 93 | except socket.error, msg: |
| 94 | raise IOError, ('socket error', msg) |
| 95 | |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 96 | # External interface |
| 97 | # retrieve(url) returns (filename, None) for a local object |
| 98 | # or (tempfilename, headers) for a remote object |
| 99 | def retrieve(self, url): |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame] | 100 | if self.tempcache.has_key(url): |
| 101 | return self.tempcache[url] |
| 102 | url1 = unwrap(url) |
| 103 | if self.tempcache.has_key(url1): |
| 104 | self.tempcache[url] = self.tempcache[url1] |
| 105 | return self.tempcache[url1] |
| 106 | type, url1 = splittype(url1) |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 107 | if not type or type == 'file': |
| 108 | try: |
| 109 | fp = self.open_local_file(url1) |
Guido van Rossum | 914973a | 1994-02-24 15:55:43 +0000 | [diff] [blame] | 110 | del fp |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 111 | return splithost(url1)[1], None |
| 112 | except IOError, msg: |
| 113 | pass |
| 114 | fp = self.open(url) |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame] | 115 | headers = fp.info() |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 116 | import tempfile |
| 117 | tfn = tempfile.mktemp() |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame] | 118 | self.tempcache[url] = result = tfn, headers |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 119 | tfp = open(tfn, 'w') |
| 120 | bs = 1024*8 |
| 121 | block = fp.read(bs) |
| 122 | while block: |
| 123 | tfp.write(block) |
| 124 | block = fp.read(bs) |
Guido van Rossum | 914973a | 1994-02-24 15:55:43 +0000 | [diff] [blame] | 125 | del fp |
| 126 | del tfp |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame] | 127 | return result |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 129 | # Each method named open_<type> knows how to open that type of URL |
| 130 | |
| 131 | # Use HTTP protocol |
| 132 | def open_http(self, url): |
| 133 | import httplib |
| 134 | host, selector = splithost(url) |
| 135 | h = httplib.HTTP(host) |
| 136 | h.putrequest('GET', selector) |
| 137 | for args in self.addheaders: apply(h.putheader, args) |
| 138 | errcode, errmsg, headers = h.getreply() |
| 139 | if errcode == 200: return addinfo(h.getfile(), headers) |
| 140 | else: raise IOError, ('http error', errcode, errmsg, headers) |
| 141 | |
| 142 | # Use Gopher protocol |
| 143 | def open_gopher(self, url): |
| 144 | import gopherlib |
| 145 | host, selector = splithost(url) |
| 146 | type, selector = splitgophertype(selector) |
| 147 | selector, query = splitquery(selector) |
| 148 | if query: fp = gopherlib.send_query(selector, query, host) |
| 149 | else: fp = gopherlib.send_selector(selector, host) |
| 150 | return addinfo(fp, noheaders()) |
| 151 | |
| 152 | # Use local file or FTP depending on form of URL |
| 153 | def open_file(self, url): |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 154 | try: |
| 155 | return self.open_local_file(url) |
| 156 | except IOError: |
| 157 | return self.open_ftp(url) |
| 158 | |
| 159 | # Use local file |
| 160 | def open_local_file(self, url): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 161 | host, file = splithost(url) |
| 162 | if not host: return addinfo(open(file, 'r'), noheaders()) |
| 163 | host, port = splitport(host) |
| 164 | if not port and socket.gethostbyname(host) in ( |
| 165 | localhost(), thishost()): |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 166 | return addinfo(open(file, 'r'), noheaders()) |
| 167 | raise IOError, ('local file error', 'not on local host') |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 168 | |
| 169 | # Use FTP protocol |
| 170 | def open_ftp(self, url): |
| 171 | host, file = splithost(url) |
Guido van Rossum | d66acb4 | 1994-03-04 12:10:33 +0000 | [diff] [blame] | 172 | if not host: raise IOError, ('ftp error', 'no host given') |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 173 | host, port = splitport(host) |
| 174 | host = socket.gethostbyname(host) |
| 175 | if not port: |
| 176 | import ftplib |
| 177 | port = ftplib.FTP_PORT |
| 178 | key = (host, port) |
| 179 | try: |
| 180 | if not self.ftpcache.has_key(key): |
| 181 | self.ftpcache[key] = ftpwrapper(host, port) |
| 182 | return addinfo(self.ftpcache[key].retrfile(file), |
| 183 | noheaders()) |
| 184 | except ftperrors(), msg: |
| 185 | raise IOError, ('ftp error', msg) |
| 186 | |
| 187 | |
| 188 | # Utility functions |
| 189 | |
| 190 | # Return the IP address of the magic hostname 'localhost' |
| 191 | _localhost = None |
| 192 | def localhost(): |
| 193 | global _localhost |
| 194 | if not _localhost: |
| 195 | _localhost = socket.gethostbyname('localhost') |
| 196 | return _localhost |
| 197 | |
| 198 | # Return the IP address of the current host |
| 199 | _thishost = None |
| 200 | def thishost(): |
| 201 | global _thishost |
| 202 | if not _thishost: |
| 203 | _thishost = socket.gethostbyname(socket.gethostname()) |
| 204 | return _thishost |
| 205 | |
| 206 | # Return the set of errors raised by the FTP class |
| 207 | _ftperrors = None |
| 208 | def ftperrors(): |
| 209 | global _ftperrors |
| 210 | if not _ftperrors: |
| 211 | import ftplib |
| 212 | _ftperrors = (ftplib.error_reply, |
| 213 | ftplib.error_temp, |
| 214 | ftplib.error_perm, |
| 215 | ftplib.error_proto) |
| 216 | return _ftperrors |
| 217 | |
| 218 | # Return an empty rfc822.Message object |
| 219 | _noheaders = None |
| 220 | def noheaders(): |
| 221 | global _noheaders |
| 222 | if not _noheaders: |
| 223 | import rfc822 |
| 224 | _noheaders = rfc822.Message(open('/dev/null', 'r')) |
| 225 | _noheaders.fp.close() # Recycle file descriptor |
| 226 | return _noheaders |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 227 | |
| 228 | |
| 229 | # Utility classes |
| 230 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 231 | # Class used by open_ftp() for cache of open FTP connections |
| 232 | class ftpwrapper: |
| 233 | def __init__(self, host, port): |
| 234 | self.host = host |
| 235 | self.port = port |
| 236 | self.init() |
| 237 | def init(self): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 238 | import ftplib |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 239 | self.ftp = ftplib.FTP() |
| 240 | self.ftp.connect(self.host, self.port) |
| 241 | self.ftp.login() |
| 242 | def retrfile(self, file): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 243 | import ftplib |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 244 | try: |
| 245 | self.ftp.voidcmd('TYPE I') |
| 246 | except ftplib.all_errors: |
| 247 | self.init() |
| 248 | self.ftp.voidcmd('TYPE I') |
| 249 | conn = None |
| 250 | if file: |
| 251 | try: |
| 252 | cmd = 'RETR ' + file |
| 253 | conn = self.ftp.transfercmd(cmd) |
| 254 | except ftplib.error_perm, reason: |
| 255 | if reason[:3] != '550': |
| 256 | raise IOError, ('ftp error', reason) |
| 257 | if not conn: |
| 258 | # Try a directory listing |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 259 | if file: cmd = 'LIST ' + file |
| 260 | else: cmd = 'LIST' |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 261 | conn = self.ftp.transfercmd(cmd) |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 262 | return addclosehook(conn.makefile('r'), self.ftp.voidresp) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 264 | # Base class for addinfo and addclosehook |
| 265 | class addbase: |
| 266 | def __init__(self, fp): |
| 267 | self.fp = fp |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 268 | self.read = self.fp.read |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 269 | self.readline = self.fp.readline |
| 270 | self.readlines = self.fp.readlines |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 271 | self.fileno = self.fp.fileno |
Guido van Rossum | 914973a | 1994-02-24 15:55:43 +0000 | [diff] [blame] | 272 | def __repr__(self): |
| 273 | return '<%s at %s whose fp = %s>' % ( |
| 274 | self.__class__.__name__, `id(self)`, `self.fp`) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 275 | def __del__(self): |
| 276 | self.close() |
| 277 | def close(self): |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 278 | self.read = None |
| 279 | self.readline = None |
| 280 | self.readlines = None |
| 281 | self.fileno = None |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 282 | self.fp = None |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 283 | |
| 284 | # Class to add a close hook to an open file |
| 285 | class addclosehook(addbase): |
| 286 | def __init__(self, fp, closehook, *hookargs): |
| 287 | addbase.__init__(self, fp) |
| 288 | self.closehook = closehook |
| 289 | self.hookargs = hookargs |
| 290 | def close(self): |
| 291 | if self.closehook: |
| 292 | apply(self.closehook, self.hookargs) |
| 293 | self.closehook = None |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 294 | self.hookargs = None |
| 295 | addbase.close(self) |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 296 | |
| 297 | # class to add an info() method to an open file |
| 298 | class addinfo(addbase): |
| 299 | def __init__(self, fp, headers): |
| 300 | addbase.__init__(self, fp) |
| 301 | self.headers = headers |
| 302 | def info(self): |
| 303 | return self.headers |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 304 | |
| 305 | |
Guido van Rossum | d1df83b | 1994-03-07 11:45:36 +0000 | [diff] [blame^] | 306 | # Utility to combine a URL with a base URL to form a new URL |
| 307 | |
| 308 | def basejoin(base, url): |
| 309 | type, path = splittype(url) |
| 310 | if type: return url |
| 311 | host, path = splithost(path) |
| 312 | basetype, basepath = splittype(base) |
| 313 | basehost, basepath = splithost(basepath) |
| 314 | basepath, basetag = splittag(basepath) |
| 315 | basepath, basequery = splitquery(basepath) |
| 316 | type = basetype or 'file' |
| 317 | if path[:1] != '/': |
| 318 | import string |
| 319 | i = string.rfind(basepath, '/') |
| 320 | if i < 0: basepath = '/' |
| 321 | else: basepath = basepath[:i+1] |
| 322 | path = basepath + path |
| 323 | if not host: host = basehost |
| 324 | if host: return type + '://' + host + path |
| 325 | else: return type + ':' + path |
| 326 | |
| 327 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 328 | # Utilities to parse URLs: |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 329 | # unwrap('<URL:type//host/path>') --> 'type//host/path' |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 330 | # splittype('type:opaquestring') --> 'type', 'opaquestring' |
| 331 | # splithost('//host[:port]/path') --> 'host[:port]', '/path' |
| 332 | # splitport('host:port') --> 'host', 'port' |
| 333 | # splitquery('/path?query') --> '/path', 'query' |
| 334 | # splittag('/path#tag') --> '/path', 'tag' |
| 335 | # splitgophertype('/Xselector') --> 'X', 'selector' |
| 336 | |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 337 | def unwrap(url): |
| 338 | import string |
| 339 | url = string.strip(url) |
| 340 | if url[:1] == '<' and url[-1:] == '>': |
| 341 | url = string.strip(url[1:-1]) |
| 342 | if url[:4] == 'URL:': url = string.strip(url[4:]) |
| 343 | return url |
| 344 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 345 | _typeprog = regex.compile('^\([^/:]+\):\(.*\)$') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 346 | def splittype(url): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 347 | if _typeprog.match(url) >= 0: return _typeprog.group(1, 2) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 348 | return None, url |
| 349 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 350 | _hostprog = regex.compile('^//\([^/]+\)\(.*\)$') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 351 | def splithost(url): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 352 | if _hostprog.match(url) >= 0: return _hostprog.group(1, 2) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 353 | return None, url |
| 354 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 355 | _portprog = regex.compile('^\(.*\):\([0-9]+\)$') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 356 | def splitport(host): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 357 | if _portprog.match(host) >= 0: return _portprog.group(1, 2) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 358 | return host, None |
| 359 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 360 | _queryprog = regex.compile('^\(.*\)\?\([^?]*\)$') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 361 | def splitquery(url): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 362 | if _queryprog.match(url) >= 0: return _queryprog.group(1, 2) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 363 | return url, None |
| 364 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 365 | _tagprog = regex.compile('^\(.*\)#\([^#]*\)$') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 366 | def splittag(url): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 367 | if _tagprog.match(url) >= 0: return _tagprog.group(1, 2) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 368 | return url, None |
| 369 | |
| 370 | def splitgophertype(selector): |
| 371 | if selector[:1] == '/' and selector[1:2]: |
| 372 | return selector[1], selector[2:] |
| 373 | return None, selector |
| 374 | |
| 375 | |
| 376 | # Test program |
| 377 | def test(): |
| 378 | import sys |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 379 | import regsub |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 380 | args = sys.argv[1:] |
| 381 | if not args: |
| 382 | args = [ |
| 383 | '/etc/passwd', |
| 384 | 'file:/etc/passwd', |
| 385 | 'file://localhost/etc/passwd', |
| 386 | 'ftp://ftp.cwi.nl/etc/passwd', |
| 387 | 'gopher://gopher.cwi.nl/11/', |
| 388 | 'http://www.cwi.nl/index.html', |
| 389 | ] |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 390 | try: |
| 391 | for url in args: |
| 392 | print '-'*10, url, '-'*10 |
| 393 | fn, h = urlretrieve(url) |
| 394 | print fn, h |
| 395 | if h: |
| 396 | print '======' |
| 397 | for k in h.keys(): print k + ':', h[k] |
| 398 | print '======' |
| 399 | fp = open(fn, 'r') |
| 400 | data = fp.read() |
Guido van Rossum | 914973a | 1994-02-24 15:55:43 +0000 | [diff] [blame] | 401 | del fp |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 402 | print regsub.gsub('\r', '', data) |
Guido van Rossum | 914973a | 1994-02-24 15:55:43 +0000 | [diff] [blame] | 403 | fn, h = None, None |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 404 | print '-'*40 |
| 405 | finally: |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame] | 406 | urlcleanup() |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 407 | |
| 408 | # Run test program when run as a script |
| 409 | if __name__ == '__main__': |
| 410 | test() |