Python 套接字在主机和虚拟机之间无法工作

标签 python sockets

我正在学习Python套接字编程。如果我在本地运行(服务器和客户端脚本),一切正常。但是,当我将服务器脚本移至虚拟机 (Ubuntu 14.04) 并从主机操作系统 (Windows 7) 运行客户端脚本时,出现以下错误:

 ConnectionRefusedError: [WinError 10061] No connection could be made because the
 target machine actively refused it

当我尝试在虚拟机内运行客户端时,它工作得很好(除非我使用 IP 地址,即:192.168.1.6,否则我必须在两个脚本中使用它)。经过我的搜索,我发现linux默认情况下并没有激活防火墙。我做错了什么?

两台机器都可以看到对方(我在它们之间设置了 Samba,可以 ping 其他机器等)。这对我来说真的很困惑。

最佳答案

也许这可以解决问题。

您的服务器代码中有一个 bind 调用,它看起来像 sock.bind(('127.0.0.1', 3333)),其中 3333 是服务器端口号。将 IP 地址更改为空字符串或 '0.0.0.0',使其看起来像 sock.bind(('', 3333))。然后再次启动客户端。

原始的bind调用仅将服务器套接字绑定(bind)到环回接口(interface)(lo),该接口(interface)仅在VM内工作。通过绑定(bind)到通配符地址('''0.0.0.0'),服务器将接受来自任何 IP 地址的连接。

关于Python 套接字在主机和虚拟机之间无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27630348/

相关文章:

c - HTTP 代理服务器保持事件连接支持

python - 导入numpy

python-3.x - Python套接字: how to detect and list connected clients in server in the other clients and how to update it when a client disconnected?

python - 远程连接到一个简单的套接字python服务器

python - 修改 Python 类

java - 如何解决 java.net.BindException : bind failed: EADDRINUSE (Address already in use)

python - 通过网络在 blender 中发送矢量对象

c# - 通过套接字发送消息的线程安全类

python - 使用 Win32com Python 中的 Range 和 Union 方法

python - 避免多次评估具有相同输入的函数