-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
19 lines (16 loc) · 868 Bytes
/
test.py
File metadata and controls
19 lines (16 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import socket
HOST = "0.0.0.0" # Standard loopback interface address (localhost)
PORT = 7525 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT)) # Bind the socket to the specified host and port
s.listen() # Start listening for incoming connections
print(f"Server listening on {HOST}:{PORT}")
conn, addr = s.accept() # Accept a new connection
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024) # Receive up to 1024 bytes of data
if not data:
break # If no data is received, the client has disconnected
conn.sendall(data) # Send the received data back to the client
print(f"Echoed: {data.decode()}") # Decode and print the echoed data