4b2a1d9a07bab2e475af63b6bbaf76e95b329fa8
[zanchey/dispense2.git] / sql-edition / servers / LATClient.py
1 from socket import *
2
3 LATCP_SOCKET = '/var/run/latlogin'
4
5 LAT_VERSION = '1.21'
6 LATCP_CMD_VERSION = 8
7 LATCP_CMD_TERMINALSESSION = 26
8 LATCP_CMD_ERRORMSG = 99
9
10 class LATClient:
11         def __init__(self, service = None, node = None, port = None, \
12                         localport = None, password = None, is_queued = False):
13                 self.sock = socket(AF_UNIX, SOCK_STREAM, 0);
14                 self.sock.connect(LATCP_SOCKET)
15                 self.send_msg(LATCP_CMD_VERSION, LAT_VERSION+'\000')
16                 (cmd, msg) = self.read_reply()
17                 if service == None: service = ''
18                 if node == None: node = ''
19                 if port == None: port = ''
20                 if localport == None: localport = ''
21                 if password == None: password = ''
22                 if is_queued == True:
23                         is_queued = 1
24                 else:
25                         is_queued = 0
26                 self.send_msg(LATCP_CMD_TERMINALSESSION, '%c%c%s%c%s%c%s%c%s%c%s' % \
27                         (is_queued,
28                          len(service), service,
29                          len(node), node,
30                          len(port), port,
31                          len(localport), localport,
32                          len(password), password
33                          ))
34                 (cmd, msg) = self.read_reply()
35                 if cmd == LATCP_CMD_ERRORMSG:
36                         raise Exception
37
38         def send_msg(self, cmd, msg):
39                 self.sock.send('%c%c%c%s'%(cmd, len(msg)/256, len(msg)%256, msg))
40
41         def read_reply(self):
42                 head = self.sock.recv(3)
43                 if len(head) != 3:
44                         sys.stderr.write('Error: Short LAT packet\n')
45                         return None
46                 cmd = head[0]
47                 length = ord(head[1])*256 + ord(head[2])
48                 msg = self.sock.recv(length)
49                 if cmd == LATCP_CMD_ERRORMSG:
50                         sys.stderr.write('Error: Received LAT error: %s\n'%msg)
51                 return (cmd, msg)
52         
53         def get_fh(self):
54                 return (self.sock.makefile('r'), self.sock.makefile('w'))

UCC git Repository :: git.ucc.asn.au