我有这些客户端和服务器代码的问题,我一直得到 [Errno 10061] No connection could be made because the target machine actively refused it
我在使用Windows XP SP3的虚拟机上运行服务器,在Windows 7 64bit上运行客户端,我的python版本是2.7.3 . 我想知道的是我应该如何编辑代码以在不同的网络上使用客户端和服务器!谢谢!
server :
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = '0.0.0.0' # Get local machine name
port = 12345 # Reserve a port for your service.
print 'Server started!'
print 'Waiting for clients...'
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
while True:
msg = c.recv(1024)
print addr, ' >> ', msg
msg = raw_input('SERVER >> ')
c.send(msg);
#c.close() # Close the connection
client :
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
print 'Connecting to ', host, port
s.connect((host, port))
while True:
msg = raw_input('CLIENT >> ')
s.send(msg)
msg = s.recv(1024)
print 'SERVER >> ', msg
#s.close # Close the socket when done
PS:代码来自互联网 .
7 回答
10061是WSAECONNREFUSED,'连接被拒绝',这意味着没有任何东西正在侦听您尝试连接的IP:端口 .
2000年左右有一个防火墙产品发出拒绝而不是忽略到被阻塞端口的传入连接,但这很快被认为是对攻击者的信息泄露并被纠正或撤回 .
提示:
actively refused
听起来有点深层次的技术问题,但.........实际上,如果在目标机器上调用bin / mongo可执行文件and the mongodb service is simply not running,也会给出此响应(特别是
errno:10061
) . 这甚至适用于本地机器实例(所有都发生在localhost上) .➪ Always rule out for this trivial possibility first ,即只需使用命令行客户端访问您的数据库 .
See here.
使用以下示例:https://docs.python.org/3.2/library/socketserver.html我确定我需要将HOST端口设置为运行服务器程序的机器 . 所以TCPServer在192.168.0.1 HOST = TCPServer IP 192.168.0.1然后我必须将TCPClient端设置为指向TCPServer IP . 所以TCPClient HOST值= 192.168.0.1 - 对不起,这是我能描述的最好的 .
如果您的计算机上安装了远程服务器 . 将server.py主机命名为“localhost”和端口号 . 那么客户端,你必须给出本地ip-127.0.0.1和端口号 . 它的作品
当我使用python库调用REST API时,我遇到了类似的问题,我发现我的服务器进入了睡眠模式,这导致了这一点 . 一旦我通过远程桌面连接登录到服务器,我的API调用就开始工作了 .
解决方案是在客户端和服务器中使用相同的IP和端口号 . 尝试,在客户端使用TCP_IP ='在这里写入ip号'TCP_PORT =在这里写入端口号s.connect((TCP_IP,TCP_PORT))
短期解决方案是分别使用默认的iis主机和端口120.0.0.1和80 . 但是我仍在寻找更通用的解决方案 .