Question

I've hosted a websocket service in my droplet, postman is able to connect to it via ip:port but not my local python app

I hosted a simple Go websocket service in port 3000, and used postman with ws://Droplet-IPV4:port/ws but when I try to connect to the same with a simple app I built on my system using python-websocket, it’s giving me this error -> Error occurred: [Errno 61] Connection refused. Please let me know if there’s a way I can connect to my server


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
June 8, 2023

Hi there,

As you are able to connect with Postman it indicates that the problem is not related to a network problem or a firewall configuration. You are running both Postman and your Python app on the same laptop right?

If this is the case, then it sounds like it might be something to do with your Python app itself, I could suggest testing this out with the following sample app and see if you get the same behaviour:

  • Install the websocket library:
pip install websocket-client
  • Here’s an example of how you might use it to connect to a WebSocket server:
import websocket
try:
    import thread
except ImportError:
    import _thread as thread
import time

def on_message(ws, message):
    print("Received message: %s" % message)

def on_error(ws, error):
    print("Error occurred: %s" % error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    print("Connection opened.")

if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://your-server-ip:3000/ws",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

This script will connect to the WebSocket server at the provided address (ws://your-server-ip:3000/ws). When it receives a message, it will print that message. If an error occurs, it will print the error. When the connection is closed, it will print a closed message. If the connection is successfully opened, it will print an opened message.

You’ll need to replace "ws://your-server-ip:3000/ws" with the address of your WebSocket server.

If you are still getting connection refused, here are some general things that you could check:

  1. Check if the service is running: SSH into your droplet and use the netstat command to check if your Go service is running and listening on the expected port:

    sudo netstat -tuln | grep 3000
    

    This should show you something like this if your service is running and listening:

    tcp6       0      0 :::3000                 :::*                    LISTEN
    

    If you don’t see any output, that means your Go service is not running or not listening on port 3000.

  2. Check firewall rules: If you’re using UFW on your droplet, you can list the firewall rules with this command:

    ufw status
    

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel