简体   繁体   English

Python套接字:连接超时

[英]Python Sockets: Connection Timeout

I'm trying to write two short python scripts that will connect two or more machines to each other, one as the server and the others as clients. 我正在尝试编写两个简短的python脚本,这些脚本将两台或更多台机器彼此连接,其中一台作为服务器,另一台作为客户端。 It worked perfectly when testing the client and the server script on the same computer, but when I tried it from another computer the client kept timing out; 在同一台计算机上测试客户端和服务器脚本时,它可以完美地工作,但是当我在另一台计算机上尝试该客户端时,客户端一直在超时。 it couldn't connect to the server. 它无法连接到服务器。 Here's my server code: 这是我的服务器代码:

import socket
server = socket.socket()
host = "computername"
port = 12345
server.bind((host, port))
server.listen(5)

client, addr = server.accept()

Client code: 客户代码:

import socket
server = socket.socket()
host = "computername"
port = 12345
server.connect((host, port))

Any clue as to why the machines can't connect? 关于为什么机器无法连接的任何线索?

I think, you are changing host variable properly when running both client and server scripts in different machines. 我认为,在不同计算机上同时运行客户端和服务器脚本时,您正在正确更改主机变量。 Try by changing that properly/or using IP address of server machine. 尝试通过适当地更改它/或使用服务器计算机的IP地址来进行。

The communication could be prohibited by a firewall. 防火墙可能禁止该通信。

To rule out DNS related problem, try IPs instead of hostnames: 要排除与DNS相关的问题,请尝试使用IP代替主机名:

# server: listen on all interfaces
server.bind(('', port))

and: 和:

# client: specify server's IP address
server.connect(("192.168.XX.YY", port))

with a real IP address, of course. 当然有一个真实的IP地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM