40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
import socket
|
|
import threading
|
|
|
|
# Define the host and port to listen on
|
|
HOST = '0.0.0.0' # Listen on all available interfaces
|
|
PORT = 443 # Port to listen on
|
|
|
|
def handle_client(client_socket, client_address):
|
|
print(f"Accepted connection from {client_address}")
|
|
with client_socket:
|
|
while True:
|
|
data = client_socket.recv(1024)
|
|
if not data:
|
|
# No more data from the client
|
|
break
|
|
# Print the received data
|
|
print(f"Received from {client_address}: {data.decode('utf-8')}")
|
|
|
|
def main():
|
|
# Create a TCP/IP socket
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
|
|
# Allow the socket to be reused
|
|
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
# Bind the socket to the host and port
|
|
server_socket.bind((HOST, PORT))
|
|
# Listen for incoming connections
|
|
server_socket.listen()
|
|
print(f"Listening on {HOST}:{PORT}")
|
|
|
|
while True:
|
|
# Accept a new connection
|
|
client_socket, client_address = server_socket.accept()
|
|
# Handle the new connection in a new thread
|
|
client_thread = threading.Thread(target=handle_client, args=(client_socket, client_address))
|
|
client_thread.daemon = True # Allow the thread to be killed when the main program exits
|
|
client_thread.start()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|