Created
December 16, 2016 15:12
-
-
Save hcastillaq/6c5c6d6ec23c22e4727e7c9c94f766c9 to your computer and use it in GitHub Desktop.
Cliente para un chat utilizando sockets en python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import threading | |
import sys | |
import pickle | |
class Cliente(): | |
"""docstring for Cliente""" | |
def __init__(self, host="localhost", port=4000): | |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.sock.connect((str(host), int(port))) | |
msg_recv = threading.Thread(target=self.msg_recv) | |
msg_recv.daemon = True | |
msg_recv.start() | |
while True: | |
msg = input('->') | |
if msg != 'salir': | |
self.send_msg(msg) | |
else: | |
self.sock.close() | |
sys.exit() | |
def msg_recv(self): | |
while True: | |
try: | |
data = self.sock.recv(1024) | |
if data: | |
print(pickle.loads(data)) | |
except: | |
pass | |
def send_msg(self, msg): | |
self.sock.send(pickle.dumps(msg)) | |
c = Cliente() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment