Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- HOST = '0.0.0.0' # Listen on all network interfaces
- PORT = 12345 # Port to listen on
- def main():
- # Create a TCP/IP socket
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
- # Bind the socket to the address and port
- server_socket.bind((HOST, PORT))
- # Listen for incoming connections
- server_socket.listen()
- print(f'Server listening on {HOST}:{PORT}')
- while True:
- # Accept incoming connection
- client_socket, client_address = server_socket.accept()
- print(f'Connection from {client_address}')
- # Handle the connection
- handle_client(client_socket)
- def handle_client(client_socket):
- # Example: Send a simple response
- response = b'Hello from the server!\n'
- client_socket.sendall(response)
- client_socket.close()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement