Skip to content

Instantly share code, notes, and snippets.

@Bogdanp
Created October 17, 2018 05:38
Show Gist options
  • Save Bogdanp/e21020eae2557baf0e81d46da2c65416 to your computer and use it in GitHub Desktop.
Save Bogdanp/e21020eae2557baf0e81d46da2c65416 to your computer and use it in GitHub Desktop.
RESPONSE = b"""\
HTTP/1.1 200 OK
Content-type: text/html
Content-length: 15
<h1>Hello!</h1>""".replace(b"\n", b"\r\n")
with socket.socket() as server_sock:
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_sock.bind((HOST, PORT))
server_sock.listen(0)
print(f"Listening on {HOST}:{PORT}...")
while True:
client_sock, client_addr = server_sock.accept()
print(f"New connection from {client_addr}.")
with client_sock:
client_sock.sendall(RESPONSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment