# Connection Refused #AP6

**URL:** <https://forum.codecrafters.io/t/connection-refused-ap6/762>\
**Category:** Challenges\
**Tags:** challenge:http-server\
**Created:** [June 18, 2024, 3:52am UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762 "2024-06-18T03:52:20Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![iExcilsior](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/iexcilsior/32/698_2.png) [@iExcilsior](https://forum.codecrafters.io/u/iExcilsior)\
**Post date:** [June 18, 2024, 3:52am UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/1 "2024-06-18T03:52:20Z")

</div>

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:

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![fre-ros](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fre-ros/32/528_2.png) [@fre-ros](https://forum.codecrafters.io/u/fre-ros)\
**Post date:** [June 18, 2024, 6:01am UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/2 "2024-06-18T06:01:04Z")

</div>

Try setting _reuse\_port_ to True in _create\_server_

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [June 18, 2024, 12:43pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/3 "2024-06-18T12:43:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![iExcilsior](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/iexcilsior/32/698_2.png) [@iExcilsior](https://forum.codecrafters.io/u/iExcilsior)\
**Post date:** [June 18, 2024, 6:02pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/4 "2024-06-18T18:02:06Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [June 18, 2024, 6:29pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/5 "2024-06-18T18:29:29Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![iExcilsior](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/iexcilsior/32/698_2.png) [@iExcilsior](https://forum.codecrafters.io/u/iExcilsior)\
**Post date:** [June 19, 2024, 9:10am UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/6 "2024-06-19T09:10:40Z")

</div>

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:

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

```

To test out my code but it showed this

```auto
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?

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [June 19, 2024, 11:18am UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/7 "2024-06-19T11:18:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![iExcilsior](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/iexcilsior/32/698_2.png) [@iExcilsior](https://forum.codecrafters.io/u/iExcilsior)\
**Post date:** [June 19, 2024, 11:58am UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/8 "2024-06-19T11:58:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [June 19, 2024, 12:02pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/9 "2024-06-19T12:02:03Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![iExcilsior](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/iexcilsior/32/698_2.png) [@iExcilsior](https://forum.codecrafters.io/u/iExcilsior)\
**Post date:** [June 19, 2024, 12:05pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/10 "2024-06-19T12:05:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [June 19, 2024, 12:12pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/11 "2024-06-19T12:12:56Z")

</div>

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

```auto
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)

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [June 19, 2024, 12:14pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/12 "2024-06-19T12:14:02Z")

</div>

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

 ![Screenshot 2024-06-19 at 13.13.53](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/1X/a25809589395b397d07f4c6ce109e12685c0b44f.png)

---

<div class="post-metadata">

**Author:** ![iExcilsior](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/iexcilsior/32/698_2.png) [@iExcilsior](https://forum.codecrafters.io/u/iExcilsior)\
**Post date:** [June 19, 2024, 12:20pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/13 "2024-06-19T12:20:57Z")

</div>

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

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/1X/b5301d754587beb3385d4fcf39077df69dcc3ae9.png)

---

<div class="post-metadata">

**Author:** ![iExcilsior](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/iexcilsior/32/698_2.png) [@iExcilsior](https://forum.codecrafters.io/u/iExcilsior)\
**Post date:** [June 19, 2024, 12:21pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/14 "2024-06-19T12:21:30Z")

</div>

![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/1X/94a07dd3490e1541c37333785681de9f5e238164.png)

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

---

<div class="post-metadata">

**Author:** ![iExcilsior](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/iexcilsior/32/698_2.png) [@iExcilsior](https://forum.codecrafters.io/u/iExcilsior)\
**Post date:** [June 19, 2024, 12:28pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/15 "2024-06-19T12:28:12Z")

</div>

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

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/1X/12846948e5ece27db64b895af148859f5b666d7d.png)

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [June 19, 2024, 12:32pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/16 "2024-06-19T12:32:07Z")

</div>

Nice. Glad all’s sorted!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [June 24, 2024, 12:32pm UTC](https://forum.codecrafters.io/t/connection-refused-ap6/762/17 "2024-06-24T12:32:26Z")

</div>

Closed on June 24, 2024, 12:32pm UTC

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