Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Gopher protocol client interface.""" |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 2 | |
Skip Montanaro | 2dd4276 | 2001-01-23 15:35:05 +0000 | [diff] [blame] | 3 | __all__ = ["send_selector","send_query"] |
| 4 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 5 | # Default selector, host and port |
| 6 | DEF_SELECTOR = '1/' |
| 7 | DEF_HOST = 'gopher.micro.umn.edu' |
| 8 | DEF_PORT = 70 |
| 9 | |
| 10 | # Recognized file types |
| 11 | A_TEXT = '0' |
| 12 | A_MENU = '1' |
| 13 | A_CSO = '2' |
| 14 | A_ERROR = '3' |
| 15 | A_MACBINHEX = '4' |
| 16 | A_PCBINHEX = '5' |
| 17 | A_UUENCODED = '6' |
| 18 | A_INDEX = '7' |
| 19 | A_TELNET = '8' |
| 20 | A_BINARY = '9' |
| 21 | A_DUPLICATE = '+' |
| 22 | A_SOUND = 's' |
| 23 | A_EVENT = 'e' |
| 24 | A_CALENDAR = 'c' |
| 25 | A_HTML = 'h' |
| 26 | A_TN3270 = 'T' |
| 27 | A_MIME = 'M' |
| 28 | A_IMAGE = 'I' |
| 29 | A_WHOIS = 'w' |
| 30 | A_QUERY = 'q' |
| 31 | A_GIF = 'g' |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 32 | A_HTML = 'h' # HTML file |
| 33 | A_WWW = 'w' # WWW address |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 34 | A_PLUS_IMAGE = ':' |
| 35 | A_PLUS_MOVIE = ';' |
| 36 | A_PLUS_SOUND = '<' |
| 37 | |
| 38 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 39 | _names = dir() |
Guido van Rossum | d2dd9a8 | 1998-01-19 21:59:48 +0000 | [diff] [blame] | 40 | _type_to_name_map = {} |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 41 | def type_to_name(gtype): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 42 | """Map all file types to strings; unknown types become TYPE='x'.""" |
| 43 | global _type_to_name_map |
| 44 | if _type_to_name_map=={}: |
| 45 | for name in _names: |
| 46 | if name[:2] == 'A_': |
| 47 | _type_to_name_map[eval(name)] = name[2:] |
| 48 | if _type_to_name_map.has_key(gtype): |
| 49 | return _type_to_name_map[gtype] |
| 50 | return 'TYPE=' + `gtype` |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 51 | |
| 52 | # Names for characters and strings |
| 53 | CRLF = '\r\n' |
| 54 | TAB = '\t' |
| 55 | |
Guido van Rossum | 2922c6d | 1994-05-06 14:28:19 +0000 | [diff] [blame] | 56 | def send_selector(selector, host, port = 0): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 57 | """Send a selector to a given host and port, return a file with the reply.""" |
| 58 | import socket |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 59 | if not port: |
Eric S. Raymond | 19e6d62 | 2001-02-09 10:10:02 +0000 | [diff] [blame] | 60 | i = host.find(':') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 61 | if i >= 0: |
Eric S. Raymond | 19e6d62 | 2001-02-09 10:10:02 +0000 | [diff] [blame] | 62 | host, port = host[:i], int(host[i+1:]) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 63 | if not port: |
| 64 | port = DEF_PORT |
| 65 | elif type(port) == type(''): |
Eric S. Raymond | 19e6d62 | 2001-02-09 10:10:02 +0000 | [diff] [blame] | 66 | port = int(port) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 67 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Guido van Rossum | 93a7c0f | 2000-03-28 21:45:46 +0000 | [diff] [blame] | 68 | s.connect((host, port)) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 69 | s.send(selector + CRLF) |
| 70 | s.shutdown(1) |
| 71 | return s.makefile('rb') |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 2922c6d | 1994-05-06 14:28:19 +0000 | [diff] [blame] | 73 | def send_query(selector, query, host, port = 0): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 74 | """Send a selector and a query string.""" |
| 75 | return send_selector(selector + '\t' + query, host, port) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | d2dd9a8 | 1998-01-19 21:59:48 +0000 | [diff] [blame] | 77 | def path_to_selector(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 78 | """Takes a path as returned by urlparse and returns the appropriate selector.""" |
| 79 | if path=="/": |
| 80 | return "/" |
| 81 | else: |
| 82 | return path[2:] # Cuts initial slash and data type identifier |
Guido van Rossum | d2dd9a8 | 1998-01-19 21:59:48 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | d2dd9a8 | 1998-01-19 21:59:48 +0000 | [diff] [blame] | 84 | def path_to_datatype_name(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 85 | """Takes a path as returned by urlparse and maps it to a string. |
| 86 | See section 3.4 of RFC 1738 for details.""" |
| 87 | if path=="/": |
| 88 | # No way to tell, although "INDEX" is likely |
| 89 | return "TYPE='unknown'" |
| 90 | else: |
| 91 | return type_to_name(path[1]) |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 93 | # The following functions interpret the data returned by the gopher |
| 94 | # server according to the expected type, e.g. textfile or directory |
| 95 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 96 | def get_directory(f): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 97 | """Get a directory in the form of a list of entries.""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 98 | list = [] |
| 99 | while 1: |
| 100 | line = f.readline() |
| 101 | if not line: |
| 102 | print '(Unexpected EOF from server)' |
| 103 | break |
| 104 | if line[-2:] == CRLF: |
| 105 | line = line[:-2] |
| 106 | elif line[-1:] in CRLF: |
| 107 | line = line[:-1] |
| 108 | if line == '.': |
| 109 | break |
| 110 | if not line: |
| 111 | print '(Empty line from server)' |
| 112 | continue |
| 113 | gtype = line[0] |
Eric S. Raymond | 19e6d62 | 2001-02-09 10:10:02 +0000 | [diff] [blame] | 114 | parts = line[1:].split(TAB) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 115 | if len(parts) < 4: |
| 116 | print '(Bad line from server:', `line`, ')' |
| 117 | continue |
| 118 | if len(parts) > 4: |
| 119 | if parts[4:] != ['+']: |
| 120 | print '(Extra info from server:', |
| 121 | print parts[4:], ')' |
| 122 | else: |
| 123 | parts.append('') |
| 124 | parts.insert(0, gtype) |
| 125 | list.append(parts) |
| 126 | return list |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 128 | def get_textfile(f): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 129 | """Get a text file as a list of lines, with trailing CRLF stripped.""" |
| 130 | list = [] |
| 131 | get_alt_textfile(f, list.append) |
| 132 | return list |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 134 | def get_alt_textfile(f, func): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 135 | """Get a text file and pass each line to a function, with trailing CRLF stripped.""" |
| 136 | while 1: |
| 137 | line = f.readline() |
| 138 | if not line: |
| 139 | print '(Unexpected EOF from server)' |
| 140 | break |
| 141 | if line[-2:] == CRLF: |
| 142 | line = line[:-2] |
| 143 | elif line[-1:] in CRLF: |
| 144 | line = line[:-1] |
| 145 | if line == '.': |
| 146 | break |
| 147 | if line[:2] == '..': |
| 148 | line = line[1:] |
| 149 | func(line) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 151 | def get_binary(f): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 152 | """Get a binary file as one solid data block.""" |
| 153 | data = f.read() |
| 154 | return data |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 156 | def get_alt_binary(f, func, blocksize): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 157 | """Get a binary file and pass each block to a function.""" |
| 158 | while 1: |
| 159 | data = f.read(blocksize) |
| 160 | if not data: |
| 161 | break |
| 162 | func(data) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 164 | def test(): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 165 | """Trivial test program.""" |
| 166 | import sys |
| 167 | import getopt |
| 168 | opts, args = getopt.getopt(sys.argv[1:], '') |
| 169 | selector = DEF_SELECTOR |
| 170 | type = selector[0] |
| 171 | host = DEF_HOST |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 172 | if args: |
| 173 | host = args[0] |
| 174 | args = args[1:] |
| 175 | if args: |
| 176 | type = args[0] |
| 177 | args = args[1:] |
| 178 | if len(type) > 1: |
| 179 | type, selector = type[0], type |
| 180 | else: |
| 181 | selector = '' |
| 182 | if args: |
| 183 | selector = args[0] |
| 184 | args = args[1:] |
| 185 | query = '' |
| 186 | if args: |
| 187 | query = args[0] |
| 188 | args = args[1:] |
| 189 | if type == A_INDEX: |
| 190 | f = send_query(selector, query, host) |
| 191 | else: |
| 192 | f = send_selector(selector, host) |
| 193 | if type == A_TEXT: |
| 194 | list = get_textfile(f) |
| 195 | for item in list: print item |
| 196 | elif type in (A_MENU, A_INDEX): |
| 197 | list = get_directory(f) |
| 198 | for item in list: print item |
| 199 | else: |
| 200 | data = get_binary(f) |
| 201 | print 'binary data:', len(data), 'bytes:', `data[:100]`[:40] |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 202 | |
| 203 | # Run the test when run as script |
| 204 | if __name__ == '__main__': |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 205 | test() |