Chat (Client-Server) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Chat (Client-Server) (/thread-20694.html) |
Chat (Client-Server) - andresdrr - Aug-26-2019 Hi guys. I have the following code (in Python 3.7.3):
################################################################################################### ### Librerias ### ################# import socket import threading import sys ################################################################################################### ### Servidor ### ################ class Server: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) connections = [] def __init__(self): self.sock.bind(('0.0.0.0', 10000)) self.sock.listen(1) def handler(self,c,a): while True: data = c.recv(1024) for connection in self.connections: connection.send(data) if not data: print(str(a[0]) + ':' + str(a[1]), "Desconectado") self.connections.remove(c) c.close() break def run(self): while True: c,a = self.sock.accept() cThread = threading.Thread(target=self.handler, args=(c,a)) cThread.daemon = True cThread.start() self.connections.append(c) print(str(a[0]) + ':' + str(a[1]), "Conectado") ################################################################################################### ### Cliente ### ############### class Client: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def sendMsg(self): while True: self.sock.send(bytes(input(""),'utf-8')) def __init__(self): self.sock.connect((address, 10000)) iThread = threading.Thread(target=self.sendMsg) iThread.daemon = True iThread.start() while True: data = self.sock.recv(1024) if not data: break print(data) ################################################################################################### ### Ejecucion ### ################# if (len(sys.argv) > 1): client = Client(sys.argv(1)) else: server = Server() server.run() ###################################################################################################The Server code runs OK: [Image: TQetpLI.png] But the Client code does not: [Image: s9xKfIg.png] I am following this Video Tutorial: Thank you very much for your help. RE: Chat (Client-Server) - Malt - Aug-26-2019 sys.argv stores the values in list structure, hence to access it we have use index ################################################################################################### ### Ejecucion ### ################# if (len(sys.argv) > 1): client = Client(sys.argv(1))Modify that to, client = Client(sys.argv[1]) RE: Chat (Client-Server) - andresdrr - Aug-26-2019 (Aug-26-2019, 05:04 AM)Malt Wrote: sys.argv stores the values in list structure, hence to access it we have use index Thank you. Problem solved but now I get another error: [Image: c0u8gP9.png] RE: Chat (Client-Server) - ThomasL - Aug-26-2019 You are creating an instance of class Client with one argument client = Client(sys.argv[1])And your Client class initialization is defined as: class Client: def __init__(self): self.sock.connect((address, 10000))It seems you want to create the Client instance with the IP address so the __init__ needs to be modified: class Client: def __init__(self, address): self.sock.connect((address, 10000)) |