So I made a simple client / server script using python sockets, and it's been working great on my PC. I decided I'd try to move the server over to a raspberry pi so it would be dedicated, and it's not working out so great.


import socket
from modules import *

def server_program():
    host = socket.gethostbyname(socket.gethostname())
    port = 5000 

    server_socket = socket.socket()
    server_socket.bind((host, port))
    print("Server is online. Awaiting commands. ")

    while True:
        conn, address = server_socket.accept()
        print("Connection from: " + str(address))
        data = conn.recv(1024).decode()
        print("\tReceived data:", data)

        execute_data(data, server_socket)

if __name__ == '__main__':


import socket

def client_program():
    # host = socket.gethostname()  # local device: This works on both devices
    # host = ''        # working computer: This works on my PC
    host = ''          # raspberry pi: This does NOT work on my Pi
    port = 5000

    client_socket = socket.socket()
    client_socket.connect((host, port))

    message = "roku remote back"

    data = client_socket.recv(8).decode() 

    print('Received from server: ' + data) 


if __name__ == '__main__':
        pass # This is where client_program() was, I moved it out to see the full error
    except ConnectionRefusedError:
        print("The server doesn't appear to be online.\n")

I'm getting this connection error, but I really don't understand why. I'm not super conversant in RaspPi, so I'm sure there's something I'm missing. Thanks for any and all help!

  • Connection refused has three possible causes. 1. Wrong IP. 2. Right IP, wrong port. 3. Right IP, right port, listener not listening.
    – Dougie
    Jan 15, 2021 at 20:48
  • I just double-checked the IP and port, so I'm assuming the listener isn't listening. Is there a reason why the same code successfully listens on my PC but not on my Pi?
    – JShoe
    Jan 16, 2021 at 1:54

1 Answer 1


Well, I have no idea why or how this was the issue, but following the advice of another post, changing host in server.py to '' fixed it.

  • What host to fix?
    – Ingo
    Jan 16, 2021 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.