bonjour j'ai reformulé le code de mon serveur en python en supprimant certains points pour ne garder que l'essentiel, je rappelle que le but de mon serveur est de recevoir les mesages des clients et de les transmettre aà tous les autres clients connectés. J'en suis arrivé à ceci: code du serveur:
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 | # import required modules import sys # to manage outpouts and error messages import socket # to create the chat server import select # to manage the send methode # main variables HOST = '' # we will use 'localhost' USERS_LIST = [] # to have a base for connected users RECV_BUFFER = 4096 # max buffering PORT = 7777 # remote port def server(): # setting up of the server server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind((HOST, PORT)) server_socket.listen(10) USERS_LIST.append(server_socket) print('Starting up the chat server on : {0}').format(PORT) while 1: # while the server is running we try to intercept incoming connections read, write, error = select.select(USERS_LIST, [], [], 0) for sock in read: if sock == server_socket: conn, addr = server_socket.accept() USERS_LIST.append(conn) print('New connected user (%s, %s)' % addr) send(server_socket, conn, "[%s:%s] entered the chatroom\n" % conn) else: try: data = sock.recv(RECV_BUFFER) if data: send(server_socket, sock, '\r' + '<' + str(sock.getpeername()) + '>' + data) else: if sock in USERS_LIST: USERS_LIST.remove(sock) send(server_socket, sock, 'The user (%s, %s) left the chatroom\n' % addr) except: send(server_socket, sock, 'The user (%s, %s) left the chatroom\n' % addr) continue server_socket.close() def send(server, sock, data): for socket in USERS_LIST: if socket != server and socket != sock: try: socket.send(data) except: socket.close() if socket in USERS_LIST: USERS_LIST.remove(socket) if __name__ == '__main__': sys.exit(server()) |
et le code du 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 | import sys import socket import select def client(): if (len(sys.argv) < 3): print('Usage : python chat_client.py hostname port') sys.exit() HOST = sys.argv[1] PORT = sys.argv[2] sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(2) try: # try to connect to the server sock.connect((HOST, PORT)) # tuple host;port except: # if there is a socket error... print('Unable to connect to the remote host: {0}').format(HOST) sys.exit() print("Now connected to the remote host: {0}").format(HOST) sys.stdout.write('<you> ') sys.stdout.flush() while 1: socket.list = [sys.stdin, sock] read, write, error = select.select(socket_list, [], []) for s in read: if s == sock: data = s.recv(4096) if not data: print('disconnected from the server') sys.exit() else: sys.stdout.write(data) sys.stdout.write('<you> ') sys.stdout.flush() else: data = sys.stdin.readline() sock.send(data) sys.stdout.write('<you> ') sys.stdout.flush() if __name__ == '__main__': sys.exit(client()) |
alors j'aimerais savoir si le code est correct car je ne peut pas le run, je suis sous mac os x avec pycharm et j'aurais besoin d'utiliser sys.argv pour donner host et port à mon client comme vous pouvez le voir sur le code du client.
mais sur pycharm il n'y a pas de fonction à ma connaissance pouvant réaliser ceci donc j'ai essayé de run le script sur terminal mais j'ai une erreur…
voici :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Unable to connect to the remote host: {0} Traceback (most recent call last): File "chat_client.py", line 20, in client sock.connect((HOST, PORT)) # tuple host;port TypeError: an integer is required (got type str) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "chat_client.py", line 53, in <module> sys.exit(client()) File "chat_client.py", line 22, in client print('Unable to connect to the remote host: {0}').format(HOST) AttributeError: 'NoneType' object has no attribute 'format' |
voilà si quelqu'un a une solution pour tester ce script… Merci bonne soirée