Tras ver que estos módulos al parecer facilitan bastante la comunicación entre cliente y servidor me decidí a ver que tal funcionaban y este es el resultado.
Servidor:
Servidor:
import SocketServer host, port = ("127.0.0.1",6565) class threadingTcpHandler(SocketServer.BaseRequestHandler): def handle(self): while True: data = self.request.recv(1024) if not data: print "client close conexion" break else: print str(self.client_address)+": "+str(data) self.request.send(data) if __name__ == '__main__': server = SocketServer.ThreadingTCPServer((host,port),threadingTcpHandler) server.allow_reuse_address = True server.serve_forever()cliente:
import asyncore,socket,time,threading class async(threading.Thread): def __init__(self,root): threading.Thread.__init__(self) self.root = root def run(self): while True: if self.waiting() == False: text = raw_input(">>>") else: continue self.change(text) def waiting(self): return self.root.wait def change(self,data): self.root.setBuffer(data) class client(asyncore.dispatcher): def __init__(self,host,port): asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET,socket.SOCK_STREAM) self.connect((host,port)) self.buffer = "" self.wait = False def setBuffer(self,data): try: self.wait = True self.buffer = data except: print "error" def handle_connect(self): pass def handle_close(self): print "close conexion" self.close() def handle_read(self): print "recivido: "+self.recv(8192) self.wait = False def writable(self): w = len(self.buffer) return (w > 0) def handle_write(self): sent = self.send(self.buffer) self.buffer = self.buffer[sent:] c = client("localhost",6565) obj = async(c) obj.daemon = True obj.start() asyncore.loop(0.3)
0 comentarios:
Publicar un comentario