Not going to lie, I changed my port before to 1142 because I was testing out what would happen if I changed my port and now that I changed it back to 4221, it won’t work anymore. Note that 4221 is the port I’m supposed to be connected to
Here are my logs:
remote: Running tests on your code. Logs should appear shortly...
remote:
remote: [tester::#AP6] Running tests for Stage #AP6 (Return a file)
remote: [tester::#AP6] $ ./your_server.sh --directory /tmp/data/codecrafters.io/http-server-tester/
remote: [tester::#AP6] Testing existing file
remote: [your_program] Server is starting...
remote: [your_program] Waiting for a connection...
remote: [your_program]
remote: [tester::#AP6] Failed to create connection: dial tcp [::1]:4221: connect: connection refused
remote: [tester::#AP6] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)
And here’s a snippet of my code:
import socket
import threading
import os
def read_files(data, connection, address):
method = data.split(' ')[0]
if method == 'GET':
directory_files = os.listdir('tmp')
requested_file = data.split(' ')[1][5::]
if requested_file in directory_files:
file_path = os.path.join('tmp', requested_file)
with open(file_path, 'rb') as file:
content = file.read()
file_size = len(content)
header = f"HTTP/1.1 200 OK \r\nContent-Type: application/octet-stream\r\nContent-Length: {file_size}\r\n\r\n".encode()
connection.sendall(header)
connection.sendall(content)
else:
connection.sendall(b"HTTP/1.1 400 Not Found\r\n\r\n")
def handle_connection(connection, address):
try:
data = connection.recv(1024).decode("utf-8")
path = data.split('\r\n')
read_files(data, connection, address)
finally:
connection.close()
def main():
server_socket = socket.create_server(('localhost', 4221))
server_socket.listen()
print('\nThe server is starting..')
print('Waiting for a connection..\n')
count = 0
while count < 1:
connection, address = server_socket.accept()
client_handler = threading.Thread(target=handle_connection, args=(connection, address))
client_handler.start()
count += 1
if __name__ == "__main__":
main()
You shouldn’t need reuse_port, should work without it.
@iExcilsior Just changing the port and back should’ve worked as long as other code didn’t change. Can you try comparing to a previous commit that passed and seeing what changed from there?
oh hey! am I also supposed to run codecrafters test? I’m running it on git bash but it says codecrafters: command not found. Sorry, I’m not knowledgeable around these.
To https://git.codecrafters.io/735c9452fbfe568d
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://git.codecrafters.io/735c9452fbfe568d'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Ahh, makes sense. I did do as you instructed. I did reset it, did the commands that include ‘-f’ and all but it still failed to make a connection. What else can I try?