gopherlib.py 5.58 KB
Newer Older
1
"""Gopher protocol client interface."""
2

3 4
__all__ = ["send_selector","send_query"]

5
import warnings
6 7
warnings.warn("the gopherlib module is deprecated", DeprecationWarning,
              stacklevel=2)
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
# Default selector, host and port
DEF_SELECTOR = '1/'
DEF_HOST     = 'gopher.micro.umn.edu'
DEF_PORT     = 70

# Recognized file types
A_TEXT       = '0'
A_MENU       = '1'
A_CSO        = '2'
A_ERROR      = '3'
A_MACBINHEX  = '4'
A_PCBINHEX   = '5'
A_UUENCODED  = '6'
A_INDEX      = '7'
A_TELNET     = '8'
A_BINARY     = '9'
A_DUPLICATE  = '+'
A_SOUND      = 's'
A_EVENT      = 'e'
A_CALENDAR   = 'c'
A_HTML       = 'h'
A_TN3270     = 'T'
A_MIME       = 'M'
A_IMAGE      = 'I'
A_WHOIS      = 'w'
A_QUERY      = 'q'
A_GIF        = 'g'
36 37
A_HTML       = 'h'          # HTML file
A_WWW        = 'w'          # WWW address
38 39 40 41 42 43
A_PLUS_IMAGE = ':'
A_PLUS_MOVIE = ';'
A_PLUS_SOUND = '<'


_names = dir()
44
_type_to_name_map = {}
45
def type_to_name(gtype):
46 47 48 49 50 51
    """Map all file types to strings; unknown types become TYPE='x'."""
    global _type_to_name_map
    if _type_to_name_map=={}:
        for name in _names:
            if name[:2] == 'A_':
                _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,)
55 56 57 58 59

# Names for characters and strings
CRLF = '\r\n'
TAB = '\t'

60
def send_selector(selector, host, port = 0):
61 62 63
    """Send a selector to a given host and port, return a file with the reply."""
    import socket
    if not port:
64
        i = host.find(':')
65
        if i >= 0:
66
            host, port = host[:i], int(host[i+1:])
67 68 69
    if not port:
        port = DEF_PORT
    elif type(port) == type(''):
70
        port = int(port)
71
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Guido van Rossum's avatar
Guido van Rossum committed
72
    s.connect((host, port))
73
    s.sendall(selector + CRLF)
74 75 76
    s.shutdown(1)
    return s.makefile('rb')

77
def send_query(selector, query, host, port = 0):
78 79
    """Send a selector and a query string."""
    return send_selector(selector + '\t' + query, host, port)
80

81
def path_to_selector(path):
82 83 84 85 86
    """Takes a path as returned by urlparse and returns the appropriate selector."""
    if path=="/":
        return "/"
    else:
        return path[2:] # Cuts initial slash and data type identifier
87 88

def path_to_datatype_name(path):
89 90 91 92 93 94 95
    """Takes a path as returned by urlparse and maps it to a string.
    See section 3.4 of RFC 1738 for details."""
    if path=="/":
        # No way to tell, although "INDEX" is likely
        return "TYPE='unknown'"
    else:
        return type_to_name(path[1])
96

97 98 99 100
# The following functions interpret the data returned by the gopher
# server according to the expected type, e.g. textfile or directory

def get_directory(f):
101
    """Get a directory in the form of a list of entries."""
102
    entries = []
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    while 1:
        line = f.readline()
        if not line:
            print '(Unexpected EOF from server)'
            break
        if line[-2:] == CRLF:
            line = line[:-2]
        elif line[-1:] in CRLF:
            line = line[:-1]
        if line == '.':
            break
        if not line:
            print '(Empty line from server)'
            continue
        gtype = line[0]
118
        parts = line[1:].split(TAB)
119
        if len(parts) < 4:
120
            print '(Bad line from server: %r)' % (line,)
121 122 123 124 125 126 127 128
            continue
        if len(parts) > 4:
            if parts[4:] != ['+']:
                print '(Extra info from server:',
                print parts[4:], ')'
        else:
            parts.append('')
        parts.insert(0, gtype)
129 130
        entries.append(parts)
    return entries
131

132
def get_textfile(f):
133
    """Get a text file as a list of lines, with trailing CRLF stripped."""
134 135 136
    lines = []
    get_alt_textfile(f, lines.append)
    return lines
137 138

def get_alt_textfile(f, func):
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    """Get a text file and pass each line to a function, with trailing CRLF stripped."""
    while 1:
        line = f.readline()
        if not line:
            print '(Unexpected EOF from server)'
            break
        if line[-2:] == CRLF:
            line = line[:-2]
        elif line[-1:] in CRLF:
            line = line[:-1]
        if line == '.':
            break
        if line[:2] == '..':
            line = line[1:]
        func(line)

155
def get_binary(f):
156 157 158
    """Get a binary file as one solid data block."""
    data = f.read()
    return data
159 160

def get_alt_binary(f, func, blocksize):
161 162 163 164 165 166
    """Get a binary file and pass each block to a function."""
    while 1:
        data = f.read(blocksize)
        if not data:
            break
        func(data)
167 168

def test():
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    """Trivial test program."""
    import sys
    import getopt
    opts, args = getopt.getopt(sys.argv[1:], '')
    selector = DEF_SELECTOR
    type = selector[0]
    host = DEF_HOST
    if args:
        host = args[0]
        args = args[1:]
    if args:
        type = args[0]
        args = args[1:]
        if len(type) > 1:
            type, selector = type[0], type
        else:
            selector = ''
            if args:
                selector = args[0]
                args = args[1:]
        query = ''
        if args:
            query = args[0]
            args = args[1:]
    if type == A_INDEX:
        f = send_query(selector, query, host)
    else:
        f = send_selector(selector, host)
    if type == A_TEXT:
198 199
        lines = get_textfile(f)
        for item in lines: print item
200
    elif type in (A_MENU, A_INDEX):
201 202
        entries = get_directory(f)
        for item in entries: print item
203 204
    else:
        data = get_binary(f)
205
        print 'binary data:', len(data), 'bytes:', repr(data[:100])[:40]
206 207 208

# Run the test when run as script
if __name__ == '__main__':
209
    test()