rpj
rpj

Reputation: 1

WebSocket client can only connect to my server on localhost

I've been playing with websockets, and I've made a little client that I've put up on a website. When I try to connect from the website ( from my computer ) to a simple Python server on the same computer it works fine, connecting to localhost. However I cannot connect from another computer. I've seen this problem in quite a few questions here, but no solutions. According to this website the problem probably can't be solved.

An alternative is socket.io, apparently, how can I use this for a client on a webhost? The GitHub socket.io page is quite vague on installation, at least for someone like me.

Well a simple example which works locally would be;

# Python server
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 12345))
sock.listen(5)

newsock, addr = sock.accept()

print "Connection"

// Client
<script type = "text/javascript">

window.onload = function()
{

    // Works
    var sock = new WebSocket("ws://127.0.0.1:12345");

    // Doesn't work
    var sock = new WebSocket("ws://194.237.*.*:12345");
}
</script>

Upvotes: 0

Views: 3407

Answers (2)

Ferus
Ferus

Reputation: 1118

For me, the problem was that I had not opened the firewall for incoming connections to python.

Upvotes: 0

Ilia Choly
Ilia Choly

Reputation: 18557

Have you tried binding to 0.0.0.0 ?

sock.bind(("0.0.0.0", 12345))

Upvotes: 4

Related Questions