def main(): #Getting here implies all pre-requisites have been met (NOT FINISHED!) accountName = options.a ipAddress = options.i port = int(options.p) request = {} response = {} request["accountName"] = accountName request["pin"] = getCardPin() response["account"] = accountName if options.n: request["action"] = "new" request["amount"] = float(options.n) response["initial_balance"] = float(options.n) elif options.d: request["action"] = "deposit" request["amount"] = float(options.d) response["deposit"] = float(options.d) elif options.w: request["action"] = "withdraw" request["amount"] = float(options.w) response["withdraw"] = float(options.w) elif options.g: request["action"] = "get" request = {"request" : request} # print >> sys.stderr, request sys.stdout.flush() message = NetMsg(request) encodedMessage = message.encryptedJson(getAuthKey(), message.getJson()) # Create socket and send data to bank sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((ipAddress, port)) sock.sendall(encodedMessage) sock.settimeout(10.0) received = sock.recv(1024) sock.settimeout(None) received = json.loads(NetMsg.decryptJson(getAuthKey(), received)) if received["msg"] == "transaction_completed": if options.g: response["balance"] = received["balance"] print json.dumps(response) sys.stdout.flush() elif received["msg"] == "transaction_failed": sys.exit(255) elif received["msg"] == "protocol_error": sys.exit(63) except socket.error, e: print >> sys.stderr, e sys.stdout.flush() sys.exit(63)
def send_ack(self, ack): ack = NetMsg.encryptedJson(auth_key, ack) self.request.sendall(ack)