-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
38 lines (29 loc) · 1.16 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import socket
def serve(debug):
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
if debug == True:
print("Your Computer Name is: " + hostname)
print("Your Computer IP Address is: " + IPAddr)
# Define the host and port to listen on
HOST = '127.0.0.1' # localhost
PORT = 12345 # Arbitrary non-privileged port
# Create a socket object
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# Bind the socket to the address and port
s.bind((HOST, PORT))
# Listen for incoming connections
s.listen()
print(f"Server listening on {HOST}:{PORT}")
# Accept incoming connections
conn, addr = s.accept()
print(f"Connection from {addr} established.")
# Receive data from the client and send a response
with conn:
while True:
data = conn.recv(1024)
if not data:
break
print(f"Received message from {addr}: {data.decode()}")
# Send a response back to the client
conn.sendall(b"Message received by server.")