To write a python program for creating Echo Client and Echo Server using TCP Sockets Links.
- Import the necessary modules in python
- Create a socket connection to using the socket module.
- Send message to the client and receive the message from the client using the Socket module in server .
- Send and receive the message using the send function in socket.
import socket
HOST = '127.0.0.1'
PORT = 65432
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while True:
message = input("Enter message to send to server: ")
s.sendall(message.encode())
data = s.recv(1024)
print('Received', repr(data.decode()))
import socket
HOST = '127.0.0.1'
PORT = 65432
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
Thus, the python program for creating Echo Client and Echo Server using TCP Sockets Links was successfully created and executed.