Question: I need help writing this in python. I need to send a string from client to server directly and encode the string. I then need
I need help writing this in python. I need to send a string from client to server directly and encode the string. I then need the server to send back the decoded string to the client. I have some of the code that needs to be edited and provided below.
For example:
1. Client wants to send "ABC" to server. The string is encoded by shifting 3 characters, and therefore "DEF" is sent to server. You may also want to use a special character, e.g., 1, to notify server that this is an encoded string. In the end, the client sends "1DEF" to server.
2. Server decodes the received string and sends back the original string to client. A special character, e.g., 0, is used to notify that it is a decoded string. That is, the server send "0ABC" back to client.
editable code:
Server:
import socket TCP_IP = '129.32.100.223' TCP_PORT = 1234 BUFFER_SIZE = 20 # Normally 1024, but we want fast response s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((TCP_IP, TCP_PORT)) s.listen(1) conn, addr = s.accept() print('Connection address:', addr) while 1: data = conn.recv(BUFFER_SIZE) if not data: break print("received data:", data.decode()) conn.send(data) # echo conn.close()
Client:
import socket
TCP_IP = '129.32.100.223' TCP_PORT = 1234 BUFFER_SIZE = 1024 MESSAGE = "Hello, World!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TCP_IP, TCP_PORT)) s.send(MESSAGE.encode()) data = s.recv(BUFFER_SIZE) s.close() print("received data:", data.decode())
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
