WebSocket 客户端只能连接到本地主机上的我的服务器

发布于 2024-12-08 17:34:01 字数 817 浏览 0 评论 0原文

我一直在玩网络套接字,并且制作了一个小客户端,并将其发布在网站上。当我尝试从网站(从我的计算机)连接到同一台计算机上的简单 Python 服务器时,它工作正常,连接到本地主机。但是我无法从另一台计算机连接。我在这里的很多问题中都看到了这个问题,但没有解决方案。根据网站,问题可能无法解决。

另一种选择是socket.io,显然,我如何将它用于网络主机上的客户端? GitHub socket.io 页面在安装方面相当模糊,至少对于像我这样的人来说是这样。

一个在本地工作的简单例子是:

# 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2024-12-15 17:34:01

您是否尝试过绑定到 0.0.0.0

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

Have you tried binding to 0.0.0.0 ?

sock.bind(("0.0.0.0", 12345))
烛影斜 2024-12-15 17:34:01

对我来说,问题是我没有打开 python 传入连接的防火墙。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文