-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3. Server.py
19 lines (15 loc) · 836 Bytes
/
3. Server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import socket
HOST = 'localhost' # The server's hostname or IP address
PORT = 8080 # The port used by the server
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)
# This code creates a simple server that listens on port 8080 and echoes back any data received from a client. When a client connects, the server prints a message indicating the connection, and then enters a loop where it waits for data to be received. When data is received, the server sends it back to the client. The connection is closed when the client sends an empty message (i.e., not data).