Connection Refused #AP6

I’m stuck on Stage #AP6

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()

Try setting reuse_port to True in create_server

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?

hey man, thanks for responding! I did check my previous commit, nothing really changed much except when I changed my port (which is now redundant)

The last successful commit I see is 8490399afe079bcd51da2b31c6e7c83261c0671b. Could you try resetting to this and running tests?

git reset --soft 8490399afe079bcd51da2b31c6e7c83261c0671b
codecrafters test

The --soft flag ensures that no changes are committed.

If that code works, then you should be able to work your back and figure out what change caused this recent issue.

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.

But I did execute the commands and ran these:

git add .
git commit --allow-empty -m "pass stage"
git push origin master

To test out my code but it showed this

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.

What do I do next?

1 Like

Try the same with git push -f origin master (when you do “git reset”, your git history changes, and you need to use -f which stands for --force)

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?

Are you sure you followed the commands exactly? I’m looking at logs and I see that different code was pushed.

To clarify, here’s what you need to run:

git reset --soft 8490399afe079bcd51da2b31c6e7c83261c0671b
git add . 
git commit -m "something"
git push -f origin master

Oh I see, that’s my fault. I just finished following the commands. It still failed to connect

Still doesn’t look like the correct commands were executed.

git reset --soft 8490399afe079bcd51da2b31c6e7c83261c0671b
git status
git add . 
git commit -m "something"
git push -f origin master

Can you run each of these and share the exact output you see? (Terminal screenshot will do)

I’m particularly interested in the output of git status. That should show multiple files changed. Here’s what I see locally:

Hello, I did as you instructed. Here are the screenshots of my terminal

since I have an extra file (test.py) I’ll try to migrate the code inside that to my main.py. I’ll see if that works

Oh damn, I think it worked. I think it connects now

1 Like

Nice. Glad all’s sorted!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.