Created
October 17, 2018 05:42
-
-
Save Bogdanp/d9afaf0dd0aa6a140bad987f6419f2fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NOT_FOUND_RESPONSE = b"""\ | |
HTTP/1.1 404 Not Found | |
Content-type: text/plain | |
Content-length: 9 | |
Not Found""".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"Received connection from {client_addr}...") | |
with client_sock: | |
try: | |
request = Request.from_socket(client_sock) | |
print(request) | |
client_sock.sendall(NOT_FOUND_RESPONSE) | |
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