QDAirPortTestSystemBackend/tcp_server.py
2026-01-28 09:56:53 +08:00

34 lines
1.1 KiB
Python

import socket
def tcp_server():
#host = '127.0.0.1' # Localhost
host = '0.0.0.0' # Localhost
port = 8082 # Port to listen on
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen(5) # Listen for up to 5 queued connections
print(f"Server listening on {host}:{port}")
while True:
client_socket, client_address = server_socket.accept()
print(f"Connection established with {client_address}")
try:
# Receive data from the client
data = client_socket.recv(1024).decode('utf-8')
print(f"Received from client: {data}")
# Send a response back to the client
response = "Hello from the server!"
client_socket.send(response.encode('utf-8'))
except Exception as e:
print(f"Error handling client: {e}")
finally:
client_socket.close()
print(f"Connection with {client_address} closed.")
if __name__ == "__main__":
tcp_server()