I’m stuck on Stage #EJ5.
I’ve tried code from other users, and the error remains even though I can confirm that the response is not “”. Any tips on how to fix will greatly appreciated. I would love to complete this project.
I’ve tried using oha
with the request from the error message and everything looks like it works fine.
Here are my logs(I’ve been trying to debug for days, excuse the messy code):
remote: Running tests on your code. Logs should appear shortly...
remote:
remote: [compile] Compilation successful.
remote:
remote: Debug = true
remote:
remote: [tester::#EJ5] Running tests for Stage #EJ5 (Concurrent connections)
remote: [tester::#EJ5] Running program
remote: [tester::#EJ5] $ ./your_server.sh
remote: [tester::#EJ5] Creating 2 parallel connections
remote: [tester::#EJ5] Creating connection 1
remote: [tester::#EJ5] Creating connection 2
remote: [tester::#EJ5] Sending first set of requests
remote: [tester::#EJ5] client-2: $ curl -v http://localhost:4221/
remote: [tester::#EJ5] client-2: > GET / HTTP/1.1
remote: [tester::#EJ5] client-2: > Host: localhost:4221
remote: [tester::#EJ5] client-2: >
remote: [tester::#EJ5] client-2: Sent bytes: "GET / HTTP/1.1\r\nHost: localhost:4221\r\n\r\n"
remote: [tester::#EJ5] Failed to read response:
remote: [tester::#EJ5] Received: "" (no content received)
remote: [tester::#EJ5] ^ error
remote: [tester::#EJ5] Error: Expected: HTTP-version, Received: ""
remote: [tester::#EJ5] Test failed
remote: [tester::#EJ5] Terminating program
remote: [tester::#EJ5] Program terminated successfully
remote:
remote: Try our CLI to run tests faster without Git: https://codecrafters.io/cli
remote:
remote: View our article on debugging test failures: https://codecrafters.io/debug
remote:
To https://git.codecrafters.io/28ff28027b4a73c7
91bbdfe..e761749 master -> master
And here’s a snippet of my code:
import socket
from threading import Thread
def responder(server_socket):
client, adress = server_socket.accept()
# Call the recv method with 1024 as the buffer size.
# Then decode the return value.
decoded_request = client.recv(1024).decode()
# Split the request by the CRLF.
request_list = decoded_request.split("\r\n")
url_path = request_list[0].split()[1]
if 'echo' in url_path.casefold():
echo_str: str = request_list[0].split()[1].split('/echo/')[1]
response = f"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: {len(echo_str)}\r\n\r\n{echo_str}".encode()
client.send(response)
# client.close()
print(type(response), 'echo')
elif 'foobar/1.2.3' in request_list[2].casefold():
user_agent: str = request_list[2].split('User-Agent: ')[1]
print(request_list[2])
response = f"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: {len(user_agent)}\r\n\r\n{user_agent}".encode()
client.send(response)
# client.close()
print(type(response), 'user-')
else:
# Encoding the response.
ok = "HTTP/1.1 200 OK\r\n\r\n".encode()
not_found = "HTTP/1.1 404 Not Found\r\n\r\n".encode()
# Send 404 if url_path is not '/' else 200.
client.send(not_found) if url_path != '/' else client.send(ok)
print(not_found) if url_path != '/' else print(ok)
# client.close()
def main():
# Creating a Socket(TCP server) bound to the address localhost:4221
# (listening on 4221)
server_socket = socket.create_server(("localhost", 4221))
# Wait for an incoming connection & returns the tuple below
# --> (client_request_obj, address info).
while True:
responder(server_socket)
thread = Thread(target=responder(server_socket))
thread.start()
if __name__ == "__main__":
main()