Skip to content

Instantly share code, notes, and snippets.

@Bogdanp
Created October 17, 2018 05:43
Show Gist options
  • Save Bogdanp/faccee7c2bb8fbdf694cb9b992bdbc80 to your computer and use it in GitHub Desktop.
Save Bogdanp/faccee7c2bb8fbdf694cb9b992bdbc80 to your computer and use it in GitHub Desktop.
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"Received connection from {client_addr}...")
with client_sock:
try:
request = Request.from_socket(client_sock)
if request.method != "GET":
client_sock.sendall(METHOD_NOT_ALLOWED_RESPONSE)
continue
serve_file(client_sock, request.path)
except Exception as e:
print(f"Failed to parse request: {e}")
client_sock.sendall(BAD_REQUEST_RESPONSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment