Yoni Meller
Yoni Meller

Reputation: 3

Python websockets server works on localhost but not on server

I made a basic chat client and server using python websockets, ran it on my pc and it worked completely fine, when I uploaded it to my windows server machine (which has the port '12345' forwarded) and tried to access it using a client from my pc I got a ConnectionRefusedError

I've tried switching to a different port (which was also forwarded) but it didn't change the result

The client (this is the bit that caused the error)

ip = input("IP Address: ")
port = int(input("Port: "))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))

The server

def open_socket(PORT:int, MAX_USERS:int):
    new_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ("localhost", PORT)
    new_socket.bind(server_address)
    new_socket.listen(MAX_USERS)
    return new_socket

Here's the error:

Traceback (most recent call last):
  File "client.py", line 24, in <module>
    sock.connect((ip, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

EDIT: After trying out Jin's answer I'm now getting a timeout error at the same place (line 24 in client.py) EDIT #2: It is now working! I changed the port to the original one (12345) and I successfully connected to the server!

Upvotes: 0

Views: 3420

Answers (1)

Jin
Jin

Reputation: 304

  1. Even though you changed a port forward config in your router, you still need to check whether your server is accepting incoming traffic in the firewall setting. You can do this from Control Panel-Security or Windows Firewall (Sorry I don't remember the exact name of the menu of the Windows).

  2. You should bind your IP with the socket, not localhost. You would want to programmatically get your IP address, rather than using hard-coded one. The followed link would help.

Finding local IP addresses using Python's stdlib

Upvotes: 1

Related Questions