【发布时间】:2020-09-01 12:24:35
【问题描述】:
我正在尝试通过 python 套接字将客户端连接到服务器。客户端在我的电脑上连接成功,但其他网络上的人无法连接。
TimeoutError: [WinError 10060]
连接失败后在客户端。这是我的 python 脚本的代码。
服务器.py
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "0.0.0.0"
port = 8000
print (host)
print (port)
serversocket.bind((host, port))
客户端.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "##.###.###.###" # server's public ip.
port = 8001
s.connect((host,port))
print("Connected to server.")
s.send(("Client connected").encode())
端口不同,因为如果路由器上的本地端口和外部端口不同,我将无法连接;本地为8000,外部为8001。如果客户端在我的计算机上,则服务器显示它正在通过公共 ip 连接,因此它正在通过我的路由器。
在我的服务器计算机上执行nmap -p 8000 -sN -P0 192.168.0.#,显示端口已使用 http-alt 关闭。在我的公共 ip 上执行此操作表明它已使用 vcom-tunnel 打开|过滤,但它几乎在每个端口上都已打开|过滤。
我尝试过的:
- local_port=8000 和 remote_port=8001 的端口转发。外部IP设置为我的公共IP,我的本地IP设置为我的计算机IP; 192.168.0.#
- 在服务器端路由器上使用 DMZ 主机(暴露主机)。
- 在服务器端路由器上分别启用和禁用 UPNP、WAN 阻塞、Ipsec PassThrough 和 PPTP PassThrough。
- 允许 TCP 端口
8000,在本地和远程通过服务器端防火墙。 - 允许程序通过客户端和服务器上公共和专用网络上的防火墙。
- 允许 TCP 端口
8001,在本地和远程通过客户端防火墙。 - 在client.py上用
socket.bind((client_ip,port))绑定客户端ip
这可能是我使用的端口与 http-alt/vcom-tunnel 相关,而不是与 upnp 相关的端口。
【问题讨论】:
标签: python python-3.x sockets networking python-sockets