def test_commsParse(self): message = 'test' response = "0009{0}\x00".format(message) reply, status = comms.parseMessage(response) self.assertEqual(status, comms.OK, \ 'Status was not OK') self.assertEqual(reply, message, \ 'Message was \"{0}\" not \"{1}\"'.format(reply, message))
def handle(self, sock, clientAddr, callAdd): data = sock.recv(comms.RECV_SIZE) if not data or len(data) < 1: return data, status = comms.parseMessage(data) if callAdd != False: addAs, shouldAdd = callAdd(data, \ service.NO_DUMP, clientAddr) if shouldAdd == comms.OK: self.sockets[addAs["id"]] = sock
def test_ListenRecvExit(self): server = ListenRecvExit() serverProcess, port = genericServer.StartServer(server) # Create the udp socket sock = comms.udp() # Send the message to the server comms.sendMessage(sock, 'test', comms.OK, (genericServer.ADDR, port)) # Receive a message response, serverAddr = sock.recvfrom(comms.RECV_SIZE) # Parse the response and return it reply, status = comms.parseMessage(response) # Stop the server serverProcess.terminate() # If the test errored self.assertEqual(EXIT_MESSAGE, reply, \ 'Server reply is not exit message {0}'.format(reply)) self.assertEqual(comms.EXIT, status, \ 'Server did not exit, status code {0}'.format(status))
def send(self, message, addr, status=comms.OK, \ waitResponse=True, useSocket=False): """ Sends a message to an address and port. Returns the response string as well as the status code. Usage: res, status = client.send("localhost", 10000, "test message") """ sock = self.sock if useSocket != False: sock = useSocket # Send the properly formated message to the client comms.sendMessage(sock, message, status, addr) # If we dont need to wait for a response return if waitResponse != True: return "", comms.OK # Receive a message response, serverAddr = sock.recvfrom(comms.RECV_SIZE) # Parse the response and return it return comms.parseMessage(response)