I’m stuck on stage 4. I’m not sure what I’m doing wrong, and I’m basically flailing at this point.
Here are my logs:
remote: Running tests on your code. Logs should appear shortly...
remote:
remote: [stage-1] Running tests for Stage #1: Bind to a port
remote: [stage-1] $ ./your_server.sh
remote: [stage-1] Connecting to localhost:4221 using TCP
remote: [your_program] Logs from your program will appear here!
remote: [stage-1] Success! Closing connection
remote: [stage-1] Test passed.
remote:
remote: [stage-2] Running tests for Stage #2: Respond with 200
remote: [stage-2] $ ./your_server.sh
remote: [stage-2] You can use the following curl command to test this locally
remote: [stage-2] $ curl -v -X GET http://localhost:4221/
remote: [stage-2] Sending request (status line): GET / HTTP/1.1
remote: [your_program] Logs from your program will appear here!
remote: [your_program] Connected by ('127.0.0.1', 52418)
remote: [stage-2] Test passed.
remote:
remote: [stage-3] Running tests for Stage #3: Respond with 404
remote: [stage-3] $ ./your_server.sh
remote: [stage-3] You can use the following curl command to test this locally
remote: [stage-3] $ curl -v -X GET http://localhost:4221/vanilla/dooby-humpty
remote: [stage-3] Sending request (status line): GET /vanilla/dooby-humpty HTTP/1.1
remote: [your_program] Logs from your program will appear here!
remote: [your_program] Connected by ('127.0.0.1', 52514)
remote: [stage-3] Test passed.
remote:
remote: [stage-4] Running tests for Stage #4: Respond with content
remote: [stage-4] $ ./your_server.sh
remote: [stage-4] You can use the following curl command to test this locally
remote: [stage-4] $ curl -v -X GET http://localhost:4221/echo/Horsey/humpty-scooby
remote: [stage-4] Sending request (status line): GET /echo/Horsey/humpty-scooby HTTP/1.1
remote: [your_program] Logs from your program will appear here!
remote: [your_program] Connected by ('127.0.0.1', 52610)
remote: [stage-4] Failed to dump response: 'context deadline exceeded (Client.Timeout or context cancellation while reading body)'
remote: [stage-4] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)
And here’s a snippet of my code:
# Uncomment this to pass the first stage
import socket
def main():
# You can use print statements as follows for debugging, they'll be visible when running tests.
print("Logs from your program will appear here!")
# Uncomment this to pass the first stage
#
server_socket = socket.create_server(("localhost", 4221), reuse_port=True)
with server_socket:
conn, addr = server_socket.accept() # wait for client
with conn:
print(f'Connected by {addr}')
while True:
response = 'HTTP/1.1 404 Not Found\r\n\r\n'
request_content = conn.recv(4096).decode()
lines = request_content.split('\r\n')
line = lines[0]
if line.startswith('GET'):
_, path, _ = line.split(' ')
if path == '/':
response = 'HTTP/1.1 200 OK\r\n\r\n'
elif path.startswith('/echo'):
subpath = path[6:]
computed_length = len(subpath)
response = 'HTTP/1.1 200 OK\r\n\r\n'
response += 'Content-Type text/plain\r\n\r\n'
response += f'Content-Length: {computed_length}\r\n\r\n'
response += f'{subpath}\r\n\r\n'
conn.sendall(response.encode())
if __name__ == "__main__":
main()