0

Client - Server in Python Sockets

Will this Client + Server Work? CLIENT: import socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect('localhost', 8820) while True: msg = input("Send To Server:\n") client_socket.send(msg.encode()) data = client_socket.recv(1024).decode() print("Server Said: " + data) if data == 'Quit': break client_socket.close() SERVER: import socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind('0.0.0.0', 8820) server_socket.listen(1) print("Listening...") conn_w_client, addr = server_socket.accept() print(f"Connected to {addr}") while True: data = conn_w_client.recv(1024).decode() print("Client Said: " + data) if data == 'Bye': conn_w_client.send('Quit'.encode()) break conn_w_client.send(data.upper().encode()) conn_w_client.close() server_socket.close()

23rd Mar 2021, 10:21 AM
Yahel
Yahel - avatar
4 Respostas
0
oh and you may want to look up encoding and decoding from text to bytes and vice versa.
23rd Mar 2021, 10:44 AM
Slick
Slick - avatar
0
This is a simple clinet/server script. What is the actual question? have you tried it?
23rd Mar 2021, 10:38 AM
Slick
Slick - avatar
0
Slick I'm afraid that if I run it and something is wrong it might not close the server and it will keep running.. so I wanna make sure that everything is good before I try and run it.. what us your opinion on the code? Thanks.
23rd Mar 2021, 10:39 AM
Yahel
Yahel - avatar
0
That will never be a problem. Its all code you run as a process. Processes can be easily terminated, especially in an ide enviornment. Or you can just press the little 'x' on the top right of the terminal, when it tells you you still have processes running, tell it you don't care.
23rd Mar 2021, 10:41 AM
Slick
Slick - avatar