home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / gopherlib.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  6.2 KB  |  221 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. __all__ = [
  5.     'send_selector',
  6.     'send_query']
  7. DEF_SELECTOR = '1/'
  8. DEF_HOST = 'gopher.micro.umn.edu'
  9. DEF_PORT = 70
  10. A_TEXT = '0'
  11. A_MENU = '1'
  12. A_CSO = '2'
  13. A_ERROR = '3'
  14. A_MACBINHEX = '4'
  15. A_PCBINHEX = '5'
  16. A_UUENCODED = '6'
  17. A_INDEX = '7'
  18. A_TELNET = '8'
  19. A_BINARY = '9'
  20. A_DUPLICATE = '+'
  21. A_SOUND = 's'
  22. A_EVENT = 'e'
  23. A_CALENDAR = 'c'
  24. A_HTML = 'h'
  25. A_TN3270 = 'T'
  26. A_MIME = 'M'
  27. A_IMAGE = 'I'
  28. A_WHOIS = 'w'
  29. A_QUERY = 'q'
  30. A_GIF = 'g'
  31. A_HTML = 'h'
  32. A_WWW = 'w'
  33. A_PLUS_IMAGE = ':'
  34. A_PLUS_MOVIE = ';'
  35. A_PLUS_SOUND = '<'
  36. _names = dir()
  37. _type_to_name_map = { }
  38.  
  39. def type_to_name(gtype):
  40.     if _type_to_name_map == { }:
  41.         for name in _names:
  42.             if name[:2] == 'A_':
  43.                 _type_to_name_map[eval(name)] = name[2:]
  44.             
  45.         
  46.     
  47.     if _type_to_name_map.has_key(gtype):
  48.         return _type_to_name_map[gtype]
  49.     
  50.     return 'TYPE=' + `gtype`
  51.  
  52. CRLF = '\r\n'
  53. TAB = '\t'
  54.  
  55. def send_selector(selector, host, port = 0):
  56.     import socket
  57.     if not port:
  58.         i = host.find(':')
  59.         if i >= 0:
  60.             (host, port) = (host[:i], int(host[i + 1:]))
  61.         
  62.     
  63.     if not port:
  64.         port = DEF_PORT
  65.     elif type(port) == type(''):
  66.         port = int(port)
  67.     
  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')
  73.  
  74.  
  75. def send_query(selector, query, host, port = 0):
  76.     return send_selector(selector + '\t' + query, host, port)
  77.  
  78.  
  79. def path_to_selector(path):
  80.     if path == '/':
  81.         return '/'
  82.     else:
  83.         return path[2:]
  84.  
  85.  
  86. def path_to_datatype_name(path):
  87.     if path == '/':
  88.         return "TYPE='unknown'"
  89.     else:
  90.         return type_to_name(path[1])
  91.  
  92.  
  93. def get_directory(f):
  94.     list = []
  95.     while 1:
  96.         line = f.readline()
  97.         if not line:
  98.             print '(Unexpected EOF from server)'
  99.             break
  100.         
  101.         if line[-2:] == CRLF:
  102.             line = line[:-2]
  103.         elif line[-1:] in CRLF:
  104.             line = line[:-1]
  105.         
  106.         if line == '.':
  107.             break
  108.         
  109.         if not line:
  110.             print '(Empty line from server)'
  111.             continue
  112.         
  113.         gtype = line[0]
  114.         parts = line[1:].split(TAB)
  115.         if len(parts) < 4:
  116.             print '(Bad line from server:', `line`, ')'
  117.             continue
  118.         
  119.         if len(parts) > 4:
  120.             if parts[4:] != [
  121.                 '+']:
  122.                 print '(Extra info from server:', parts[4:], ')'
  123.             
  124.         else:
  125.             parts.append('')
  126.         parts.insert(0, gtype)
  127.         list.append(parts)
  128.     return list
  129.  
  130.  
  131. def get_textfile(f):
  132.     list = []
  133.     get_alt_textfile(f, list.append)
  134.     return list
  135.  
  136.  
  137. def get_alt_textfile(f, func):
  138.     while 1:
  139.         line = f.readline()
  140.         if not line:
  141.             print '(Unexpected EOF from server)'
  142.             break
  143.         
  144.         if line[-2:] == CRLF:
  145.             line = line[:-2]
  146.         elif line[-1:] in CRLF:
  147.             line = line[:-1]
  148.         
  149.         if line == '.':
  150.             break
  151.         
  152.         if line[:2] == '..':
  153.             line = line[1:]
  154.         
  155.         func(line)
  156.  
  157.  
  158. def get_binary(f):
  159.     data = f.read()
  160.     return data
  161.  
  162.  
  163. def get_alt_binary(f, func, blocksize):
  164.     while 1:
  165.         data = f.read(blocksize)
  166.         if not data:
  167.             break
  168.         
  169.         func(data)
  170.  
  171.  
  172. def test():
  173.     import sys
  174.     import getopt
  175.     (opts, args) = getopt.getopt(sys.argv[1:], '')
  176.     selector = DEF_SELECTOR
  177.     type = selector[0]
  178.     host = DEF_HOST
  179.     if args:
  180.         host = args[0]
  181.         args = args[1:]
  182.     
  183.     if args:
  184.         type = args[0]
  185.         args = args[1:]
  186.         if len(type) > 1:
  187.             (type, selector) = (type[0], type)
  188.         else:
  189.             selector = ''
  190.             if args:
  191.                 selector = args[0]
  192.                 args = args[1:]
  193.             
  194.         query = ''
  195.         if args:
  196.             query = args[0]
  197.             args = args[1:]
  198.         
  199.     
  200.     if type == A_INDEX:
  201.         f = send_query(selector, query, host)
  202.     else:
  203.         f = send_selector(selector, host)
  204.     if type == A_TEXT:
  205.         list = get_textfile(f)
  206.         for item in list:
  207.             print item
  208.         
  209.     elif type in (A_MENU, A_INDEX):
  210.         list = get_directory(f)
  211.         for item in list:
  212.             print item
  213.         
  214.     else:
  215.         data = get_binary(f)
  216.         print 'binary data:', len(data), 'bytes:', `data[:100]`[:40]
  217.  
  218. if __name__ == '__main__':
  219.     test()
  220.  
  221.