blob: e91ef99eb70699e8aecc44643f0a3af452fd9155 [file] [log] [blame]
Guido van Rossum23acc951994-02-21 16:36:04 +00001# Gopher protocol client interface
2
3import string
4
5# Default selector, host and port
6DEF_SELECTOR = '1/'
7DEF_HOST = 'gopher.micro.umn.edu'
8DEF_PORT = 70
9
10# Recognized file types
11A_TEXT = '0'
12A_MENU = '1'
13A_CSO = '2'
14A_ERROR = '3'
15A_MACBINHEX = '4'
16A_PCBINHEX = '5'
17A_UUENCODED = '6'
18A_INDEX = '7'
19A_TELNET = '8'
20A_BINARY = '9'
21A_DUPLICATE = '+'
22A_SOUND = 's'
23A_EVENT = 'e'
24A_CALENDAR = 'c'
25A_HTML = 'h'
26A_TN3270 = 'T'
27A_MIME = 'M'
28A_IMAGE = 'I'
29A_WHOIS = 'w'
30A_QUERY = 'q'
31A_GIF = 'g'
32A_HTML = 'h' # HTML file
33A_WWW = 'w' # WWW address
34A_PLUS_IMAGE = ':'
35A_PLUS_MOVIE = ';'
36A_PLUS_SOUND = '<'
37
38
39# Function mapping all file types to strings; unknown types become TYPE='x'
40_names = dir()
Guido van Rossumd2dd9a81998-01-19 21:59:48 +000041_type_to_name_map = {}
Guido van Rossum23acc951994-02-21 16:36:04 +000042def type_to_name(gtype):
43 global _type_to_name_map
Guido van Rossumd2dd9a81998-01-19 21:59:48 +000044 if _type_to_name_map=={}:
Guido van Rossum23acc951994-02-21 16:36:04 +000045 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`
51
52# Names for characters and strings
53CRLF = '\r\n'
54TAB = '\t'
55
56# Send a selector to a given host and port, return a file with the reply
Guido van Rossum2922c6d1994-05-06 14:28:19 +000057def send_selector(selector, host, port = 0):
Guido van Rossum23acc951994-02-21 16:36:04 +000058 import socket
59 import string
Guido van Rossum2922c6d1994-05-06 14:28:19 +000060 if not port:
Guido van Rossum23acc951994-02-21 16:36:04 +000061 i = string.find(host, ':')
62 if i >= 0:
63 host, port = host[:i], string.atoi(host[i+1:])
64 if not port:
65 port = DEF_PORT
66 elif type(port) == type(''):
67 port = string.atoi(port)
68 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
69 s.connect(host, port)
70 s.send(selector + CRLF)
71 s.shutdown(1)
Jack Jansen2bb57b81996-02-14 16:06:24 +000072 return s.makefile('rb')
Guido van Rossum23acc951994-02-21 16:36:04 +000073
74# Send a selector and a query string
Guido van Rossum2922c6d1994-05-06 14:28:19 +000075def send_query(selector, query, host, port = 0):
76 return send_selector(selector + '\t' + query, host, port)
Guido van Rossum23acc951994-02-21 16:36:04 +000077
Guido van Rossumd2dd9a81998-01-19 21:59:48 +000078# Takes a path as returned by urlparse and returns the appropriate selector
79def path_to_selector(path):
80 if path=="/":
81 return "/"
82 else:
83 return path[2:] # Cuts initial slash and data type identifier
84
85# Takes a path as returned by urlparse and maps it to a string
86# See section 3.4 of RFC 1738 for details
87def path_to_datatype_name(path):
88 if path=="/":
89 return "TYPE='unknown'" # No way to tell, although "INDEX" is probable
90 else:
91 return type_to_name(path[1])
92
Guido van Rossum23acc951994-02-21 16:36:04 +000093# The following functions interpret the data returned by the gopher
94# server according to the expected type, e.g. textfile or directory
95
96# Get a directory in the form of a list of entries
97def get_directory(f):
98 import string
99 list = []
100 while 1:
101 line = f.readline()
102 if not line:
103 print '(Unexpected EOF from server)'
104 break
105 if line[-2:] == CRLF:
106 line = line[:-2]
107 elif line[-1:] in CRLF:
108 line = line[:-1]
109 if line == '.':
110 break
111 if not line:
112 print '(Empty line from server)'
113 continue
114 gtype = line[0]
115 parts = string.splitfields(line[1:], TAB)
116 if len(parts) < 4:
117 print '(Bad line from server:', `line`, ')'
118 continue
119 if len(parts) > 4:
120 if parts[4:] != ['+']:
121 print '(Extra info from server:', parts[4:], ')'
122 else:
123 parts.append('')
124 parts.insert(0, gtype)
125 list.append(parts)
126 return list
127
128# Get a text file as a list of lines, with trailing CRLF stripped
129def get_textfile(f):
130 list = []
131 get_alt_textfile(f, list.append)
132 return list
133
134# Get a text file and pass each line to a function, with trailing CRLF stripped
135def get_alt_textfile(f, func):
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)
150
151# Get a binary file as one solid data block
152def get_binary(f):
153 data = f.read()
154 return data
155
156# Get a binary file and pass each block to a function
157def get_alt_binary(f, func, blocksize):
158 while 1:
159 data = f.read(blocksize)
160 if not data:
161 break
162 func(data)
163
164# Trivial test program
165def test():
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
172 port = DEF_PORT
173 if args:
174 host = args[0]
175 args = args[1:]
176 if args:
177 type = args[0]
178 args = args[1:]
179 if len(type) > 1:
180 type, selector = type[0], type
181 else:
182 selector = ''
183 if args:
184 selector = args[0]
185 args = args[1:]
186 query = ''
187 if args:
188 query = args[0]
189 args = args[1:]
190 if type == A_INDEX:
191 f = send_query(selector, query, host)
192 else:
193 f = send_selector(selector, host)
194 if type == A_TEXT:
195 list = get_textfile(f)
196 for item in list: print item
197 elif type in (A_MENU, A_INDEX):
198 list = get_directory(f)
199 for item in list: print item
200 else:
201 data = get_binary(f)
202 print 'binary data:', len(data), 'bytes:', `data[:100]`[:40]
203
204# Run the test when run as script
205if __name__ == '__main__':
206 test()