class EchoCmdHandler(CmdHandler): def __init__(self): CmdHandler.__init__(self) self.echo_client = EchoClient(self) self.conn = None pass def __conn_to_server(self, ip, port): init_client(self.echo_client, ip, port) def on_conn_made(self, conn): self.conn = conn self.__conn_to_server('localhost', 8888) pass def on_conn_lost(self, conn, reason): self.conn = None def on_msg(self, conn, msg): # print 'on msg on cmd handler' self.echo_client.sendLine(msg) pass def on_client_msg(self, client, msg): self.conn.sendLine(msg) pass
class HandShapeClient(object): def __init__(self, host, port): self.client = EchoClient(host, port, self) self.converter = FrameConverter() def send_data(self, encoded_data): self.send_time = time.strftime("%H:%M:%S") self.client.send_data(encoded_data + '\n') def callback(self, server_response): logging.debug('Send: %s, %s' % (self.send_time, server_response))
class HandShapeClient(object): def __init__(self, host, port): self.client = EchoClient(host, port, self) self.converter = FrameConverter() def send_data(self, encoded_data): self.send_time = time.strftime("%H:%M:%S") self.client.send_data(encoded_data+'\n') def callback(self, server_response): logging.debug('Send: %s, %s' % (self.send_time, server_response))
def test_EchoClient(): test_ip = u'127.0.0.1' test_port = port_gen.next() test_client = EchoClient(test_ip, test_port) q = Queue.Queue() t = threading.Thread( target=run_test_server, args=(q, test_ip, test_port)) t.daemon = True t.start() time.sleep(1) # give the test server a chance to spin up test_msg = u"Hello, Test Server" assert test_client.sendMessage(test_msg) == test_msg
def do_POST(self): """Start TCP client from request.""" global DEFAULT_TIMEOUT, SERVICES, LOCAL_PORT_UDP bad_request_str = b"<html><body><h1>Bad Request!</h1></body></html>" try: form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={"REQUEST_METHOD": "POST"}) service = form.getvalue("service", None) if not service or service not in SERVICES: self._set_headers(400) self.wfile.write(bad_request_str) return request_vals = { "address": form.getvalue("address", None), "port": form.getvalue("port", None), "message": form.getvalue("message", "Hello from Flake Client!"), "echo": form.getvalue("echo", False), } flog.info(request_vals) if service in [ "tcp_client", "udp_client" ] and not (request_vals["address"] and request_vals["port"] and request_vals["port"].isdigit() and (request_vals["message"] or request_vals["echo"])): self._set_headers(400) self.wfile.write(bad_request_str) return local_port = None if service == "udp_client": LOCAL_PORT_UDP += 1 local_port = LOCAL_PORT_UDP if service in ["tcp_client", "udp_client"]: echo_client = EchoClient( address=request_vals["address"], port=int(request_vals["port"]), message=request_vals["message"], echo=request_vals["echo"], mode="TCP" if service == "tcp_client" else "UDP", local_port=local_port, timeout=DEFAULT_TIMEOUT, ) flog.info(f"Starting {service} thread.") _thread.start_new_thread(echo_client.run, ()) self._set_headers(200) self.wfile.write( b"<html><body><h1>Request Received, starting %s!</h1></body></html>" % service.encode()) except Exception as e: self._set_headers(400) self.wfile.write( b"<html><body><h1>Bad Request!</h1><br><p>%s</p></body></html>" % repr(e).encode("utf-8"))
def __init__(self): CmdHandler.__init__(self) self.echo_client = EchoClient(self) self.conn = None pass
def __init__(self): self.echo_client = EchoClient(self) self.conn = None pass
def __init__(self, host, port): self.client = EchoClient(host, port, self) self.converter = FrameConverter()
from multiprocessing.context import Process from multiprocessing.spawn import freeze_support from time import sleep from echo_client import EchoClient from echo_server import EchoServer if __name__ == '__main__': # freeze_support() s = Process(target=EchoServer) s.start() sleep(1) # need to figure out a way to make this intelligent. c = EchoClient() c.echo("Now is the time for all good men.") c.echo("You load 14 tons, and what do you get?") c.echo("!quit")