Echo server and client using python socket
To develop a simple webserver to serve html programming pages.
Design of echo server and client using python socket
Implementation using Python code
Testing the server and client
import socket
s = socket.socket()
s.connect(('localhost', 8000))
while True:
msg = input("client > ")
s.send(msg.encode())
if msg == 'exit':
print("Closing connection...")
break
print("server > ", s.recv(1024).decode())
s.close()
print("Client closed.")
import socket
s = socket.socket()
s.bind(('localhost', 8000))
s.listen(5)
print("Server is listening...")
c, addr = s.accept()
print(f"Connection from {addr} has been established.")
while True:
s_msg = c.recv(1024).decode()
if s_msg == 'exit':
print("Client requested to close the connection.")
break
print("client > ", s_msg)
c.send(s_msg.encode())
c.close()
s.close()
print("Server closed.")
The program is executed successfully