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
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
import socket
HOST = "127.0.0.1" # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b"Hello, world")
data = s.recv(1024)
print(f"Received {data!r}")
![image](https://private-user-images.githubusercontent.com/139841918/309973727-bbb00c93-8598-44f1-b23d-5334c9e563f7.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUyODAwMTUsIm5iZiI6MTcyNTI3OTcxNSwicGF0aCI6Ii8xMzk4NDE5MTgvMzA5OTczNzI3LWJiYjAwYzkzLTg1OTgtNDRmMS1iMjNkLTUzMzRjOWU1NjNmNy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwMlQxMjIxNTVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT00NDAxZGY1Y2VlZTRiZTJiNjgyMTVmYjQ3OTY3OTQ3NDliMTlhNTEyMzEyODE2ZGUxMTAwMzk1ZTY4NzkyNmM2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.8vPm078dIaCsHE5V3jsgIK5tLs3xD7RQkT3AsC8jarg)
CLIENT SIDE:
![image](https://private-user-images.githubusercontent.com/139841918/309973854-fe761599-4d85-4feb-b5d9-ae40ded342ee.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUyODAwMTUsIm5iZiI6MTcyNTI3OTcxNSwicGF0aCI6Ii8xMzk4NDE5MTgvMzA5OTczODU0LWZlNzYxNTk5LTRkODUtNGZlYi1iNWQ5LWFlNDBkZWQzNDJlZS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwMlQxMjIxNTVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01N2ZmZGIxNmM1YzdjODUxOGJkNmY0YTU0YzNiY2IyM2I5ZWIxZTJhMTQ5ZmE3OGEwNzMyYTlkYjc4M2Q3N2Y2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9._mwO6fGOg6J5_oh4tolDo8a_XUstp60-xYF1jegSvm0)
The program is executed successfully