1 """Gopher protocol client interface."""
3 __all__ = [
"send_selector",
"send_query"]
7 DEF_HOST =
'gopher.micro.umn.edu'
40 _type_to_name_map = {}
42 """Map all file types to strings; unknown types become TYPE='x'."""
43 global _type_to_name_map
44 if _type_to_name_map=={}:
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`
57 """Send a selector to a given host and port, return a file with the reply."""
62 host, port = host[:i], int(host[i+1:])
65 elif type(port) == type(
''):
68 s.connect((host, port))
69 s.sendall(selector + CRLF)
71 return s.makefile(
'rb')
74 """Send a selector and a query string."""
78 """Takes a path as returned by urlparse and returns the appropriate selector."""
85 """Takes a path as returned by urlparse and maps it to a string.
86 See section 3.4 of RFC 1738 for details."""
89 return "TYPE='unknown'"
97 """Get a directory in the form of a list of entries."""
102 print '(Unexpected EOF from server)'
104 if line[-2:] == CRLF:
106 elif line[-1:]
in CRLF:
111 print '(Empty line from server)'
114 parts = line[1:].
split(TAB)
116 print '(Bad line from server:', `line`,
')'
119 if parts[4:] != [
'+']:
120 print '(Extra info from server:',
124 parts.insert(0, gtype)
129 """Get a text file as a list of lines, with trailing CRLF stripped."""
135 """Get a text file and pass each line to a function, with trailing CRLF stripped."""
139 print '(Unexpected EOF from server)'
141 if line[-2:] == CRLF:
143 elif line[-1:]
in CRLF:
152 """Get a binary file as one solid data block."""
157 """Get a binary file and pass each block to a function."""
159 data = f.read(blocksize)
165 """Trivial test program."""
169 selector = DEF_SELECTOR
179 type, selector = type[0], type
195 for item
in list:
print item
196 elif type
in (A_MENU, A_INDEX):
198 for item
in list:
print item
201 print 'binary data:', len(data),
'bytes:', `data[:100]`[:40]
204 if __name__ ==
'__main__':