From 891e6406bd0428b82706bbbcc5d71ffc1463b18f Mon Sep 17 00:00:00 2001 From: Bernard Blackham Date: Fri, 25 Jun 2004 14:23:08 +0000 Subject: [PATCH] Initial stuff --- sql-edition/servers/LATClient.py | 54 +++++++++++++++++++++++++++++++ sql-edition/servers/VendServer.py | 44 +++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 sql-edition/servers/LATClient.py create mode 100755 sql-edition/servers/VendServer.py diff --git a/sql-edition/servers/LATClient.py b/sql-edition/servers/LATClient.py new file mode 100644 index 0000000..4b2a1d9 --- /dev/null +++ b/sql-edition/servers/LATClient.py @@ -0,0 +1,54 @@ +from socket import * + +LATCP_SOCKET = '/var/run/latlogin' + +LAT_VERSION = '1.21' +LATCP_CMD_VERSION = 8 +LATCP_CMD_TERMINALSESSION = 26 +LATCP_CMD_ERRORMSG = 99 + +class LATClient: + def __init__(self, service = None, node = None, port = None, \ + localport = None, password = None, is_queued = False): + self.sock = socket(AF_UNIX, SOCK_STREAM, 0); + self.sock.connect(LATCP_SOCKET) + self.send_msg(LATCP_CMD_VERSION, LAT_VERSION+'\000') + (cmd, msg) = self.read_reply() + if service == None: service = '' + if node == None: node = '' + if port == None: port = '' + if localport == None: localport = '' + if password == None: password = '' + if is_queued == True: + is_queued = 1 + else: + is_queued = 0 + self.send_msg(LATCP_CMD_TERMINALSESSION, '%c%c%s%c%s%c%s%c%s%c%s' % \ + (is_queued, + len(service), service, + len(node), node, + len(port), port, + len(localport), localport, + len(password), password + )) + (cmd, msg) = self.read_reply() + if cmd == LATCP_CMD_ERRORMSG: + raise Exception + + def send_msg(self, cmd, msg): + self.sock.send('%c%c%c%s'%(cmd, len(msg)/256, len(msg)%256, msg)) + + def read_reply(self): + head = self.sock.recv(3) + if len(head) != 3: + sys.stderr.write('Error: Short LAT packet\n') + return None + cmd = head[0] + length = ord(head[1])*256 + ord(head[2]) + msg = self.sock.recv(length) + if cmd == LATCP_CMD_ERRORMSG: + sys.stderr.write('Error: Received LAT error: %s\n'%msg) + return (cmd, msg) + + def get_fh(self): + return (self.sock.makefile('r'), self.sock.makefile('w')) diff --git a/sql-edition/servers/VendServer.py b/sql-edition/servers/VendServer.py new file mode 100755 index 0000000..57a2682 --- /dev/null +++ b/sql-edition/servers/VendServer.py @@ -0,0 +1,44 @@ +#!/usr/bin/python + +import sys, os, string, socket, time +from popen2 import popen2 +from pyPgSQL import PgSQL +from LATClient import LATClient + +class VendingMachine: + def __init__(self, rfh, wfh): + self.rfh = rfh + self.wfh = wfh + self.wfh.write('\n') + self.await_prompt() + + def await_prompt(self): + self.wfh.flush() + state = 0 + s = '' + while state != 3: + s = self.rfh.read(1) + if s == '': raise Exception + if s == '\n' and state == 0: state = 1 + if s == '#' and state == 1: state = 2 + if s == ' ' and state == 2: state = 3 + + def get_response(self): + self.wfh.flush() + s = '' + while s == '': + s = self.rfh.readline() + if s == '': return None + s = s.strip('\r\n') + return s + + def ping(self): + self.wfh.write('PING\n') + return self.get_response() + +if __name__ == '__main__': + # Open vending machine via LAT + latclient = LATClient(service = 'VEND', password = 'dmscptd') + (rfh, wfh) = latclient.get_fh() + v = VendingMachine(rfh, wfh) + print v.ping() -- 2.20.1