def test(self, selftest): # We need to discover SERVEP_IP and set up SERVER_PORT # Note: Port 7 is Echo Protocol: # # Port number rationale: # # The Echo Protocol is a service in the Internet Protocol Suite defined # in RFC 862. It was originally proposed for testing and measurement # of round-trip times[citation needed] in IP networks. # # A host may connect to a server that supports the Echo Protocol using # the Transmission Control Protocol (TCP) or the User Datagram Protocol # (UDP) on the well-known port number 7. The server sends back an # identical copy of the data it received. SERVER_IP = str(socket.gethostbyname(socket.getfqdn())) SERVER_PORT = 32767 # Returning none will suppress host test from printing success code server = UDPServer((SERVER_IP, SERVER_PORT), UDPEchoClient_Handler) server.allow_reuse_address = True print "HOST: Listening for UDP connections..." self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT) server.serve_forever()
except (IOError, TypeError, OSError, ValueError, IndexError), err: print "Error parsing request data: %s" % err return "ERR" class UDPHandle(BaseRequestHandler): def handle(self): addr = self.client_address sock = self.request[1] resp = "" try: raw_data = str(self.request[0]).strip().replace('\n', '') if EOF not in raw_data or SOF not in raw_data: print "Failed to pull entire stream..." sock.sendto(ON_FAIL, addr) return print "From (%s): %s" % (str(addr), raw_data) resp = data_handle(raw_data) sock.sendto(SOF + resp + EOF, addr) except (OSError, ValueError, TypeError, IOError), err: print "UDP handle error: %s" % err sock.sendto(SOF + resp + EOF, addr) if __name__ == "__main__": worx.set_thing(worx_thing) server = UDPServer((BIND_ADDR, BIND_PORT), UDPHandle) server.allow_reuse_address = True print "Starting server...\nwaiting for data..." server.serve_forever()