Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
gopherlib Namespace Reference

Functions

def type_to_name
 
def send_selector
 
def send_query
 
def path_to_selector
 
def path_to_datatype_name
 
def get_directory
 
def get_textfile
 
def get_alt_textfile
 
def get_binary
 
def get_alt_binary
 
def test
 

Variables

list __all__ = ["send_selector","send_query"]
 
string DEF_SELECTOR = '1/'
 
string DEF_HOST = 'gopher.micro.umn.edu'
 
int DEF_PORT = 70
 
string A_TEXT = '0'
 
string A_MENU = '1'
 
string A_CSO = '2'
 
string A_ERROR = '3'
 
string A_MACBINHEX = '4'
 
string A_PCBINHEX = '5'
 
string A_UUENCODED = '6'
 
string A_INDEX = '7'
 
string A_TELNET = '8'
 
string A_BINARY = '9'
 
string A_DUPLICATE = '+'
 
string A_SOUND = 's'
 
string A_EVENT = 'e'
 
string A_CALENDAR = 'c'
 
string A_HTML = 'h'
 
string A_TN3270 = 'T'
 
string A_MIME = 'M'
 
string A_IMAGE = 'I'
 
string A_WHOIS = 'w'
 
string A_QUERY = 'q'
 
string A_GIF = 'g'
 
string A_WWW = 'w'
 
string A_PLUS_IMAGE = ':'
 
string A_PLUS_MOVIE = ';'
 
string A_PLUS_SOUND = '<'
 
tuple _names = dir()
 
dictionary _type_to_name_map = {}
 
string CRLF = '\r\n'
 
string TAB = '\t'
 

Detailed Description

Gopher protocol client interface.

Function Documentation

def gopherlib.get_alt_binary (   f,
  func,
  blocksize 
)
Get a binary file and pass each block to a function.

Definition at line 156 of file gopherlib.py.

157 def get_alt_binary(f, func, blocksize):
158  """Get a binary file and pass each block to a function."""
159  while 1:
160  data = f.read(blocksize)
161  if not data:
162  break
163  func(data)
def gopherlib.get_alt_textfile (   f,
  func 
)
Get a text file and pass each line to a function, with trailing CRLF stripped.

Definition at line 134 of file gopherlib.py.

135 def get_alt_textfile(f, func):
136  """Get a text file and pass each line to a function, with trailing CRLF stripped."""
137  while 1:
138  line = f.readline()
139  if not line:
140  print '(Unexpected EOF from server)'
141  break
142  if line[-2:] == CRLF:
143  line = line[:-2]
144  elif line[-1:] in CRLF:
145  line = line[:-1]
146  if line == '.':
147  break
148  if line[:2] == '..':
149  line = line[1:]
150  func(line)
def gopherlib.get_binary (   f)
Get a binary file as one solid data block.

Definition at line 151 of file gopherlib.py.

152 def get_binary(f):
153  """Get a binary file as one solid data block."""
154  data = f.read()
155  return data
def gopherlib.get_directory (   f)
Get a directory in the form of a list of entries.

Definition at line 96 of file gopherlib.py.

References dospath.split().

96 
97 def get_directory(f):
98  """Get a directory in the form of a list of entries."""
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 = line[1:].split(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:',
122  print parts[4:], ')'
123  else:
124  parts.append('')
125  parts.insert(0, gtype)
126  list.append(parts)
127  return list
def gopherlib.get_textfile (   f)
Get a text file as a list of lines, with trailing CRLF stripped.

Definition at line 128 of file gopherlib.py.

References get_alt_textfile().

129 def get_textfile(f):
130  """Get a text file as a list of lines, with trailing CRLF stripped."""
131  list = []
132  get_alt_textfile(f, list.append)
133  return list
def gopherlib.path_to_datatype_name (   path)
Takes a path as returned by urlparse and maps it to a string.
See section 3.4 of RFC 1738 for details.

Definition at line 84 of file gopherlib.py.

References type_to_name().

84 
85 def path_to_datatype_name(path):
86  """Takes a path as returned by urlparse and maps it to a string.
87  See section 3.4 of RFC 1738 for details."""
88  if path=="/":
89  # No way to tell, although "INDEX" is likely
90  return "TYPE='unknown'"
91  else:
92  return type_to_name(path[1])
93 
94 # The following functions interpret the data returned by the gopher
95 # server according to the expected type, e.g. textfile or directory
def gopherlib.path_to_selector (   path)
Takes a path as returned by urlparse and returns the appropriate selector.

Definition at line 77 of file gopherlib.py.

77 
78 def path_to_selector(path):
79  """Takes a path as returned by urlparse and returns the appropriate selector."""
80  if path=="/":
81  return "/"
82  else:
83  return path[2:] # Cuts initial slash and data type identifier
def gopherlib.send_query (   selector,
  query,
  host,
  port = 0 
)
Send a selector and a query string.

Definition at line 73 of file gopherlib.py.

References send_selector().

73 
74 def send_query(selector, query, host, port = 0):
75  """Send a selector and a query string."""
76  return send_selector(selector + '\t' + query, host, port)
def gopherlib.send_selector (   selector,
  host,
  port = 0 
)
Send a selector to a given host and port, return a file with the reply.

Definition at line 56 of file gopherlib.py.

References socket.socket().

56 
57 def send_selector(selector, host, port = 0):
58  """Send a selector to a given host and port, return a file with the reply."""
59  import socket
60  if not port:
61  i = host.find(':')
62  if i >= 0:
63  host, port = host[:i], int(host[i+1:])
64  if not port:
65  port = DEF_PORT
66  elif type(port) == type(''):
67  port = int(port)
68  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
69  s.connect((host, port))
70  s.sendall(selector + CRLF)
71  s.shutdown(1)
72  return s.makefile('rb')
def gopherlib.test ( )
Trivial test program.

Definition at line 164 of file gopherlib.py.

References get_binary(), get_directory(), get_textfile(), getopt.getopt(), send_query(), and send_selector().

165 def test():
166  """Trivial test program."""
167  import sys
168  import getopt
169  opts, args = getopt.getopt(sys.argv[1:], '')
170  selector = DEF_SELECTOR
171  type = selector[0]
172  host = DEF_HOST
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 
# Run the test when run as script
def gopherlib.type_to_name (   gtype)
Map all file types to strings; unknown types become TYPE='x'.

Definition at line 41 of file gopherlib.py.

41 
42 def type_to_name(gtype):
43  """Map all file types to strings; unknown types become TYPE='x'."""
44  global _type_to_name_map
45  if _type_to_name_map=={}:
46  for name in _names:
47  if name[:2] == 'A_':
48  _type_to_name_map[eval(name)] = name[2:]
49  if _type_to_name_map.has_key(gtype):
50  return _type_to_name_map[gtype]
51  return 'TYPE=' + `gtype`
52 
# Names for characters and strings

Variable Documentation

list __all__ = ["send_selector","send_query"]

Definition at line 3 of file gopherlib.py.

tuple _names = dir()

Definition at line 39 of file gopherlib.py.

dictionary _type_to_name_map = {}

Definition at line 40 of file gopherlib.py.

string A_BINARY = '9'

Definition at line 20 of file gopherlib.py.

string A_CALENDAR = 'c'

Definition at line 24 of file gopherlib.py.

string A_CSO = '2'

Definition at line 13 of file gopherlib.py.

string A_DUPLICATE = '+'

Definition at line 21 of file gopherlib.py.

string A_ERROR = '3'

Definition at line 14 of file gopherlib.py.

string A_EVENT = 'e'

Definition at line 23 of file gopherlib.py.

string A_GIF = 'g'

Definition at line 31 of file gopherlib.py.

string A_HTML = 'h'

Definition at line 25 of file gopherlib.py.

string A_IMAGE = 'I'

Definition at line 28 of file gopherlib.py.

string A_INDEX = '7'

Definition at line 18 of file gopherlib.py.

string A_MACBINHEX = '4'

Definition at line 15 of file gopherlib.py.

string A_MENU = '1'

Definition at line 12 of file gopherlib.py.

string A_MIME = 'M'

Definition at line 27 of file gopherlib.py.

string A_PCBINHEX = '5'

Definition at line 16 of file gopherlib.py.

string A_PLUS_IMAGE = ':'

Definition at line 34 of file gopherlib.py.

string A_PLUS_MOVIE = ';'

Definition at line 35 of file gopherlib.py.

string A_PLUS_SOUND = '<'

Definition at line 36 of file gopherlib.py.

string A_QUERY = 'q'

Definition at line 30 of file gopherlib.py.

string A_SOUND = 's'

Definition at line 22 of file gopherlib.py.

string A_TELNET = '8'

Definition at line 19 of file gopherlib.py.

string A_TEXT = '0'

Definition at line 11 of file gopherlib.py.

string A_TN3270 = 'T'

Definition at line 26 of file gopherlib.py.

string A_UUENCODED = '6'

Definition at line 17 of file gopherlib.py.

string A_WHOIS = 'w'

Definition at line 29 of file gopherlib.py.

string A_WWW = 'w'

Definition at line 33 of file gopherlib.py.

string CRLF = '\r\n'

Definition at line 53 of file gopherlib.py.

string DEF_HOST = 'gopher.micro.umn.edu'

Definition at line 7 of file gopherlib.py.

int DEF_PORT = 70

Definition at line 8 of file gopherlib.py.

string DEF_SELECTOR = '1/'

Definition at line 6 of file gopherlib.py.

string TAB = '\t'

Definition at line 54 of file gopherlib.py.