bonjour j'ai regardé des tutoriels sur les serveurs de chat python mais ma fonction qui envoie à tous les clients que le client s'est déconnecté n'envoie rien tandis que sur les vidéos des exemples sa marche, quand un client ferme sa console alors qu'il est connecté au serveur et ben le serveur affiche client(x) déconnecté et envoie cette donnée à tous les clients connectés si quelqu'un trouve la faille merci de me le dire car après ce problème mon projet est terminé.
voici le code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | import socket import sys import threading import select _HOST = '192.168.1.22' _PORT = 4567 _BUFFER = 1024 _RUNNING = True _USERS = [] print('+++++++++ ADRENALIN CHAT SERVER ++++++++++\n' '+ made by: backtrackO5 +\n' '+ AdrenalinZTeam +\n' '+ contact me: +\n' '+ backtrack@gmail.com +\n' '+ +\n' '+ ENJOY IT ! +\n' '++++++++++++++++++++++++++++++++++++++++++\n') class Server(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.host = socket.gethostbyname(_HOST) self.port = int(_PORT) self.buffer = _BUFFER self.run = _RUNNING self.users = _USERS self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def broadcast_data(self, sock, data): for sockt in self.users: if sockt != self.sock and sockt != sock: try: sockt.send(data.encode()) except: sockt.close() self.users.remove(sockt) def main(self): try: self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind((self.host, self.port)) self.sock.listen(10) self.users.append(self.sock) except socket.error: print('Unable to connect ot the remote host: {0}'.format(self.host)) sys.exit() print('Chat server started on port: ' + str(self.port) + '\r') while 1: read_sockets, write_sockets, in_error = select.select(self.users, [], []) for sockp in read_sockets: if sockp == self.sock: sockfd, addr = self.sock.accept() self.users.append(sockfd) print('Client ({0}) connected\r'.format(addr)) self.broadcast_data(sockfd, '[{0}] entered the chatroom\r'.format(addr)) else: try: data = sockp.recv(self.buffer).decode() if data: print('\r' + str(sockp.getpeername()) + data) self.broadcast_data(sockp, '\r' + '(' + str(sockp.getpeername()) + ')' + ' ' + data) else: if sockp in self.users: self.users.remove(sockp) self.broadcast_data(sockp, 'Client({0}) is offline\r'.format(addr)) except socket.error: self.broadcast_data(sockp, 'Client({0}) is offline\r'.format(addr)) print('Client({0}) is offline\r'.format(addr)) continue self.sock.close() if __name__ == '__main__': server = Server() server.main() |
et le client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import sqlite3 import sys import socket import select import hashlib ##################### if len(sys.argv) < 3: print('[usage] python client.py <hostname> <port> <nickname>') sys.exit() HOST = str(sys.argv[1]) PORT = int(sys.argv[2]) #USERNAME = str(sys.argv[3]) #PASSWORD = hashlib.md5(sys.argv[4].encode()).hexdigest() print('+++++++++ ADRENALIN CHAT SERVER ++++++++++\n' '+ made by: backtrackO5 +\n' '+ AdrenalinZTeam +\n' '+ contact me: +\n' '+ backtrack@gmail.com +\n' '+ +\n' '+ ENJOY IT ! +\n' '++++++++++++++++++++++++++++++++++++++++++\n') class Client: def __init__(self): self.host = HOST self.port = PORT self.run = True self.buff = 1024 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(2) def prompt(self): sys.stdout.write('<you> ') sys.stdout.flush() def main(self): try: self.sock.connect((self.host, self.port)) except socket.error: print('Unable to connect to the remote host: {0}'.format(self.host)) sys.exit() print('connected to the remote host. Start sending messages') self.prompt() while 1: rlist = [sys.stdin, self.sock] read_list, write_list, error_list = select.select(rlist, [], []) for sock in read_list: if sock == self.sock: data = self.sock.recv(self.buff).decode() if not data: print('\nDisconnected from chat server') sys.exit() if data: sys.stdout.write(data) self.prompt() else: data = sys.stdin.readline() self.sock.send(data.encode()) self.prompt() if __name__ == '__main__': client = Client() client.main() |
merci
+0
-0