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) |
| 172 | host, port = splitport(host) |
| 173 | host = socket.gethostbyname(host) |
| 174 | if not port: |
| 175 | import ftplib |
| 176 | port = ftplib.FTP_PORT |
| 177 | key = (host, port) |
| 178 | try: |
| 179 | if not self.ftpcache.has_key(key): |
| 180 | self.ftpcache[key] = ftpwrapper(host, port) |
| 181 | return addinfo(self.ftpcache[key].retrfile(file), |
| 182 | noheaders()) |
| 183 | except ftperrors(), msg: |
| 184 | raise IOError, ('ftp error', msg) |
| 185 | |
| 186 | |
| 187 | # Utility functions |
| 188 | |
| 189 | # Return the IP address of the magic hostname 'localhost' |
| 190 | _localhost = None |
| 191 | def localhost(): |
| 192 | global _localhost |
| 193 | if not _localhost: |
| 194 | _localhost = socket.gethostbyname('localhost') |
| 195 | return _localhost |
| 196 | |
| 197 | # Return the IP address of the current host |
| 198 | _thishost = None |
| 199 | def thishost(): |
| 200 | global _thishost |
| 201 | if not _thishost: |
| 202 | _thishost = socket.gethostbyname(socket.gethostname()) |
| 203 | return _thishost |
| 204 | |
| 205 | # Return the set of errors raised by the FTP class |
| 206 | _ftperrors = None |
| 207 | def ftperrors(): |
| 208 | global _ftperrors |
| 209 | if not _ftperrors: |
| 210 | import ftplib |
| 211 | _ftperrors = (ftplib.error_reply, |
| 212 | ftplib.error_temp, |
| 213 | ftplib.error_perm, |
| 214 | ftplib.error_proto) |
| 215 | return _ftperrors |
| 216 | |
| 217 | # Return an empty rfc822.Message object |
| 218 | _noheaders = None |
| 219 | def noheaders(): |
| 220 | global _noheaders |
| 221 | if not _noheaders: |
| 222 | import rfc822 |
| 223 | _noheaders = rfc822.Message(open('/dev/null', 'r')) |
| 224 | _noheaders.fp.close() # Recycle file descriptor |
| 225 | return _noheaders |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 226 | |
| 227 | |
| 228 | # Utility classes |
| 229 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 230 | # Class used by open_ftp() for cache of open FTP connections |
| 231 | class ftpwrapper: |
| 232 | def __init__(self, host, port): |
| 233 | self.host = host |
| 234 | self.port = port |
| 235 | self.init() |
| 236 | def init(self): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 237 | import ftplib |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 238 | self.ftp = ftplib.FTP() |
| 239 | self.ftp.connect(self.host, self.port) |
| 240 | self.ftp.login() |
| 241 | def retrfile(self, file): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 242 | import ftplib |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 243 | try: |
| 244 | self.ftp.voidcmd('TYPE I') |
| 245 | except ftplib.all_errors: |
| 246 | self.init() |
| 247 | self.ftp.voidcmd('TYPE I') |
| 248 | conn = None |
| 249 | if file: |
| 250 | try: |
| 251 | cmd = 'RETR ' + file |
| 252 | conn = self.ftp.transfercmd(cmd) |
| 253 | except ftplib.error_perm, reason: |
| 254 | if reason[:3] != '550': |
| 255 | raise IOError, ('ftp error', reason) |
| 256 | if not conn: |
| 257 | # Try a directory listing |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 258 | if file: cmd = 'LIST ' + file |
| 259 | else: cmd = 'LIST' |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 260 | conn = self.ftp.transfercmd(cmd) |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 261 | return addclosehook(conn.makefile('r'), self.ftp.voidresp) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 263 | # Base class for addinfo and addclosehook |
| 264 | class addbase: |
| 265 | def __init__(self, fp): |
| 266 | self.fp = fp |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 267 | self.read = self.fp.read |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 268 | self.readline = self.fp.readline |
| 269 | self.readlines = self.fp.readlines |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 270 | self.fileno = self.fp.fileno |
Guido van Rossum | 914973a | 1994-02-24 15:55:43 +0000 | [diff] [blame] | 271 | def __repr__(self): |
| 272 | return '<%s at %s whose fp = %s>' % ( |
| 273 | self.__class__.__name__, `id(self)`, `self.fp`) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 274 | def __del__(self): |
| 275 | self.close() |
| 276 | def close(self): |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 277 | self.read = None |
| 278 | self.readline = None |
| 279 | self.readlines = None |
| 280 | self.fileno = None |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 281 | self.fp = None |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 282 | |
| 283 | # Class to add a close hook to an open file |
| 284 | class addclosehook(addbase): |
| 285 | def __init__(self, fp, closehook, *hookargs): |
| 286 | addbase.__init__(self, fp) |
| 287 | self.closehook = closehook |
| 288 | self.hookargs = hookargs |
| 289 | def close(self): |
| 290 | if self.closehook: |
| 291 | apply(self.closehook, self.hookargs) |
| 292 | self.closehook = None |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 293 | self.hookargs = None |
| 294 | addbase.close(self) |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 295 | |
| 296 | # class to add an info() method to an open file |
| 297 | class addinfo(addbase): |
| 298 | def __init__(self, fp, headers): |
| 299 | addbase.__init__(self, fp) |
| 300 | self.headers = headers |
| 301 | def info(self): |
| 302 | return self.headers |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 303 | |
| 304 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 305 | # Utilities to parse URLs: |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 306 | # unwrap('<URL:type//host/path>') --> 'type//host/path' |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 307 | # splittype('type:opaquestring') --> 'type', 'opaquestring' |
| 308 | # splithost('//host[:port]/path') --> 'host[:port]', '/path' |
| 309 | # splitport('host:port') --> 'host', 'port' |
| 310 | # splitquery('/path?query') --> '/path', 'query' |
| 311 | # splittag('/path#tag') --> '/path', 'tag' |
| 312 | # splitgophertype('/Xselector') --> 'X', 'selector' |
| 313 | |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 314 | def unwrap(url): |
| 315 | import string |
| 316 | url = string.strip(url) |
| 317 | if url[:1] == '<' and url[-1:] == '>': |
| 318 | url = string.strip(url[1:-1]) |
| 319 | if url[:4] == 'URL:': url = string.strip(url[4:]) |
| 320 | return url |
| 321 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 322 | _typeprog = regex.compile('^\([^/:]+\):\(.*\)$') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 323 | def splittype(url): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 324 | if _typeprog.match(url) >= 0: return _typeprog.group(1, 2) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 325 | return None, url |
| 326 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 327 | _hostprog = regex.compile('^//\([^/]+\)\(.*\)$') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 328 | def splithost(url): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 329 | if _hostprog.match(url) >= 0: return _hostprog.group(1, 2) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 330 | return None, url |
| 331 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 332 | _portprog = regex.compile('^\(.*\):\([0-9]+\)$') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 333 | def splitport(host): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 334 | if _portprog.match(host) >= 0: return _portprog.group(1, 2) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 335 | return host, None |
| 336 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 337 | _queryprog = regex.compile('^\(.*\)\?\([^?]*\)$') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 338 | def splitquery(url): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 339 | if _queryprog.match(url) >= 0: return _queryprog.group(1, 2) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 340 | return url, None |
| 341 | |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 342 | _tagprog = regex.compile('^\(.*\)#\([^#]*\)$') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 343 | def splittag(url): |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 344 | if _tagprog.match(url) >= 0: return _tagprog.group(1, 2) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 345 | return url, None |
| 346 | |
| 347 | def splitgophertype(selector): |
| 348 | if selector[:1] == '/' and selector[1:2]: |
| 349 | return selector[1], selector[2:] |
| 350 | return None, selector |
| 351 | |
| 352 | |
| 353 | # Test program |
| 354 | def test(): |
| 355 | import sys |
Guido van Rossum | 749057b | 1994-02-22 19:03:38 +0000 | [diff] [blame] | 356 | import regsub |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 357 | args = sys.argv[1:] |
| 358 | if not args: |
| 359 | args = [ |
| 360 | '/etc/passwd', |
| 361 | 'file:/etc/passwd', |
| 362 | 'file://localhost/etc/passwd', |
| 363 | 'ftp://ftp.cwi.nl/etc/passwd', |
| 364 | 'gopher://gopher.cwi.nl/11/', |
| 365 | 'http://www.cwi.nl/index.html', |
| 366 | ] |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 367 | try: |
| 368 | for url in args: |
| 369 | print '-'*10, url, '-'*10 |
| 370 | fn, h = urlretrieve(url) |
| 371 | print fn, h |
| 372 | if h: |
| 373 | print '======' |
| 374 | for k in h.keys(): print k + ':', h[k] |
| 375 | print '======' |
| 376 | fp = open(fn, 'r') |
| 377 | data = fp.read() |
Guido van Rossum | 914973a | 1994-02-24 15:55:43 +0000 | [diff] [blame] | 378 | del fp |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 379 | print regsub.gsub('\r', '', data) |
Guido van Rossum | 914973a | 1994-02-24 15:55:43 +0000 | [diff] [blame] | 380 | fn, h = None, None |
Guido van Rossum | d5b9ea1 | 1994-02-24 13:50:39 +0000 | [diff] [blame] | 381 | print '-'*40 |
| 382 | finally: |
Guido van Rossum | 67e22c2 | 1994-03-02 11:28:34 +0000 | [diff] [blame^] | 383 | urlcleanup() |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 384 | |
| 385 | # Run test program when run as a script |
| 386 | if __name__ == '__main__': |
| 387 | test() |