Facebook
From asd, 2 Months ago, written in Python.
Embed
Download Paste or View Raw
Hits: 182
  1. import socket
  2.  
  3. # Create a socket object
  4. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5.  
  6. # Bind the socket to a specific address and port
  7. server_address = ('localhost', 12345)
  8. server_socket.bind(server_address)
  9.  
  10. # Listen for incoming connections (max 5 connections in the queue)
  11. server_socket.listen(5)
  12. print("Server is listening for connections...")
  13.  
  14. while True:
  15.     # Wait for a connection
  16.     client_socket, client_address = server_socket.accept()
  17.     print(f"Connection established with {client_address}")
  18.  
  19.     # Send a welcome message to the client
  20.     welcome_message = "Welcome to the server!"
  21.     client_socket.send(welcome_message.encode())
  22.  
  23.     # Close the connection with the client
  24.     client_socket.close()
  25.