Advertisement
Guest User

tupac

a guest
Oct 4th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import asyncore
  5. from socket import AF_INET, SOCK_STREAM
  6. from threading import Thread
  7.  
  8.  
  9. class TCPClient(asyncore.dispatcher_with_send):
  10.    
  11.     def __init__(self, host=("192.168.43.174", 9090)):
  12.         asyncore.dispatcher_with_send.__init__(self)
  13.         self.create_socket(AF_INET, SOCK_STREAM)
  14.         self.connect(host)
  15.        
  16.         Thread(target=self.sender).start()  # Nuevo hilo creado para el cliente
  17.    
  18.     def sender(self):
  19.         # Enviar los mensajes desde un hilo aparte,
  20.         # para que en paralelo se sigan reciban los
  21.         # mensajes del servidor.
  22.         while True:
  23.             data = raw_input("\n")
  24.             if data:
  25.                 if data.lower() == "salir":#Al recibir el mensaje de "salir" escrito, se cierra la conexion.
  26.                     break
  27.                 else:
  28.                     self.sendall(data)
  29.             else:
  30.                 print "Debes escribir un mensaje."
  31.         self.handle_close()
  32.    
  33.     def handle_connect(self):
  34.         print "Has entrado al chat."
  35.    
  36.     def handle_close(self):
  37.         print "Has salido del chat."
  38.         self.close()
  39.    
  40.     def handle_read(self):
  41.         """Imprimir los mensajes"""
  42.         print self.recv(1024)#Tamaño de datos que se recibiran.
  43.  
  44.  
  45. if __name__ == "__main__":
  46.     client = TCPClient()
  47.     asyncore.loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement