Skip to content

Instantly share code, notes, and snippets.

@Bogdanp
Created October 17, 2018 05:36
Show Gist options
  • Save Bogdanp/6e0927e9a6c833676dd712ea789b7f2f to your computer and use it in GitHub Desktop.
Save Bogdanp/6e0927e9a6c833676dd712ea789b7f2f to your computer and use it in GitHub Desktop.
import socket
HOST = "127.0.0.1"
PORT = 9000
# By default, socket.socket creates TCP sockets.
with socket.socket() as server_sock:
# This tells the kernel to reuse sockets that are in `TIME_WAIT` state.
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# This tells the socket what address to bind to.
server_sock.bind((HOST, PORT))
# 0 is the number of pending connections the socket may have before
# new connections are refused. Since this server is going to process
# one connection at a time, we want to refuse any additional connections.
server_sock.listen(0)
print(f"Listening on {HOST}:{PORT}...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment