Question: Write two programs called echo _ client.py and echo _ server.py , respectively. The echo _ client.py aims to connect and send messages to the

Write two programs called echo_client.py and echo_server.py, respectively. The
echo_client.py aims to connect and send messages to the echo server. The echo server
can echo back the same message back to the client. Partial code of echo_server.py and
echo_client.py are provided in the course blackboard. You can download them from
3
the assessment folder to complete the code.
Run echo server using the following command:
$ python3 echo_server.py
Where port_number refers to the port number that the echo server will listen to.
Run echo client using the following command:
$ python3 echo_client.py
Where port_number refers to the server port number that the client will connect to.
The echo server will send back the exact message that it receives.
The client will print the returned message to clients stdout:
$ python3 echo_client.py # run the echo client
Hello World # client sends a message to the server.
[Server] Hello World # response from the server.
ECHO_SERVER
import socket
import sys
class EchoServer:
def __init__(self):
self.addr = None
self.port = None
self.socket = None
self.host ="127.0.0.1"
self.conn = None
def read_port_number(self):
"""
Read the port number from argument, store it to self.port.
Exit with status 1 if invalid argument is provided.
"""
if len(sys.argv)!=2:
print("Usage: python echo_server.py ")
sys.exit(1)
try:
self.port = int(sys.argv[1])
except ValueError:
print("Invalid port number")
sys.exit(1)
def listen_on_port(self):
"""
Create a socket listens on the specified port.
Store the socket object to self.socket.
Store the new accepted connection to self.conn.
"""
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((self.host, self.port))
self.socket.listen(1)
print(f"Listening on port {self.port}")
self.conn, addr = self.socket.accept()
print(f"Accepted connection from {addr}")
def receive_message(self):
"""
Receive a TCP packet from the client.
:return: the received message
"""
try:
data = self.conn.recv(1024)
if not data:
print("Connection closed by the client")
sys.exit(0)
return data.decode()
except Exception as e:
print(f"Error receiving message: {e}")
return None
def send_message(self, msg):
"""
Send a message back to the client
:param msg: the message to send to the client
:return: None
"""
try:
self.conn.sendall(msg.encode())
except Exception as e:
print(f"Error sending message: {e}")
def echo_messages(self):
"""
Use a while loop to echo messages back to client
:return: None
"""
while True:
try:
data = self.receive_message()
if not data:
print("Connection closed by the client")
break
print(f"Received message: {data}")
self.send_message(data) # Send the received message back to the client
except Exception as e:
print(f"Error receiving message: {e}")
break
def run_echo_server(self):
"""
Run the echo server to receive and echo back messages
:return: None
"""
self.read_port_number()
self.listen_on_port()
self.echo_messages()
if __name__=="__main__":
echo_server = EchoServer()
echo_server.run_echo_server()
ECHO_CLIENT
import socket
import sys
class EchoClient:
def __init__(self):
self.host ="127.0.0.1"
self.port = None
self.socket = None
def read_port_number(self):
"""
Read the port number from argument, store it to self.port.
Exit if invalid argument is provided.
:return: None
"""
if len(sys.argv)!=2:
sys.exit(1)
try:
self.port = int(sys.argv[1])
except ValueError:
print("Invalid port number")
sys.exit(1)
def connect_to_port(self):
"""
Create a socket to try to connect to a specified port,
store the new socket object to self.socket.
:return: None
"""
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.socket.connect((self.host, self.port))
except Exception as e:
print(f"Error connecting to server: {e}")
sys.exit(1)
def receive_and_print_message(self):
"""
Receive a TCP packet from the server and print

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!