1 """Gopher protocol client interface."""
3 __all__ = ["send_selector","send_query"]
6 warnings.warn("the gopherlib module is deprecated", DeprecationWarning,
9 # Default selector, host and port
11 DEF_HOST = 'gopher.micro.umn.edu'
14 # Recognized file types
36 A_HTML = 'h' # HTML file
37 A_WWW = 'w' # WWW address
44 _type_to_name_map = {}
45 def type_to_name(gtype):
46 """Map all file types to strings; unknown types become TYPE='x'."""
47 global _type_to_name_map
48 if _type_to_name_map=={}:
51 _type_to_name_map[eval(name)] = name[2:]
52 if gtype in _type_to_name_map:
53 return _type_to_name_map[gtype]
54 return 'TYPE=%r' % (gtype,)
56 # Names for characters and strings
60 def send_selector(selector, host, port = 0):
61 """Send a selector to a given host and port, return a file with the reply."""
66 host, port = host[:i], int(host[i+1:])
69 elif type(port) == type(''):
71 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
72 s.connect((host, port))
73 s.sendall(selector + CRLF)
75 return s.makefile('rb')
77 def send_query(selector, query, host, port = 0):
78 """Send a selector and a query string."""
79 return send_selector(selector + '\t' + query, host, port)
81 def path_to_selector(path):
82 """Takes a path as returned by urlparse and returns the appropriate selector."""
86 return path[2:] # Cuts initial slash and data type identifier
88 def path_to_datatype_name(path):
89 """Takes a path as returned by urlparse and maps it to a string.
90 See section 3.4 of RFC 1738 for details."""
92 # No way to tell, although "INDEX" is likely
93 return "TYPE='unknown'"
95 return type_to_name(path[1])
97 # The following functions interpret the data returned by the gopher
98 # server according to the expected type, e.g. textfile or directory
100 def get_directory(f):
101 """Get a directory in the form of a list of entries."""
106 print '(Unexpected EOF from server)'
108 if line[-2:] == CRLF:
110 elif line[-1:] in CRLF:
115 print '(Empty line from server)'
118 parts = line[1:].split(TAB)
120 print '(Bad line from server: %r)' % (line,)
123 if parts[4:] != ['+']:
124 print '(Extra info from server:',
128 parts.insert(0, gtype)
129 entries.append(parts)
133 """Get a text file as a list of lines, with trailing CRLF stripped."""
135 get_alt_textfile(f, lines.append)
138 def get_alt_textfile(f, func):
139 """Get a text file and pass each line to a function, with trailing CRLF stripped."""
143 print '(Unexpected EOF from server)'
145 if line[-2:] == CRLF:
147 elif line[-1:] in CRLF:
156 """Get a binary file as one solid data block."""
160 def get_alt_binary(f, func, blocksize):
161 """Get a binary file and pass each block to a function."""
163 data = f.read(blocksize)
169 """Trivial test program."""
172 opts, args = getopt.getopt(sys.argv[1:], '')
173 selector = DEF_SELECTOR
183 type, selector = type[0], type
194 f = send_query(selector, query, host)
196 f = send_selector(selector, host)
198 lines = get_textfile(f)
199 for item in lines: print item
200 elif type in (A_MENU, A_INDEX):
201 entries = get_directory(f)
202 for item in entries: print item
205 print 'binary data:', len(data), 'bytes:', repr(data[:100])[:40]
207 # Run the test when run as script
208 if __name__ == '__main__':