Python跨虚拟机socket通信出现connexion refused(errno111)错误排查
问题描述
我编写了用于两台虚拟机间通信的Python代码,运行时遇到Python socket error: connexion refused (stack.error errno 111)错误。
我确认不存在防火墙或其他安全拦截,怀疑是否为所使用的端口存在问题,特此求助。
客户端代码
import socket import subprocess try: # on cree un objet socket # socket.AF_INET : la famille d adresses, ici ce sont des adresses Internet ; # socket.SOCK_STREAM : le type du socket, SOCK_STREAM pour le protocole TCP. s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # on defini le port port = 12345 # connection vers le serveur s.connect(('192.168.1.20', port)) # receive data from the server, 2048 bytes print s.recv(2048) # close the connection s.close() except: print("une erreur est survenue...")
服务端代码(运行在另一台虚拟机上)
import socket import subprocess import os,sys #========================================================================================== #creation d'une classe pour rendre le programme oriente objet class GetInfo: infos = os.uname() currentUser = os.getlogin() rep_actuel = os.getcwd() myDirectory = os.listdir("/home/debian/Documents") scanpath = "Liste de tous les repertoires dans Users : \n" def listInfo(self): #afficher des informations sur le systeme return GetInfo.infos #afficher l'utilisateur actuel return GetInfo.currentUser #afficher le path du repertoire actuel return GetInfo.rep_actuel def scan(self): #print "Liste de tous les repertoires dans Users/" #on liste toutes les directories et sous directories for file in GetInfo.myDirectory: GetInfo.scanpath += file + "; \n" return GetInfo.scanpath #instance de l objet GetInfo (creation de l'objet) GetMyInfo = GetInfo() # afficage des infos de l'objet #GetMyInfo.listInfo() #GetMyInfo.scan() #========================================================================================== # on cree un objet socket s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) print "Socket successfully created" # reservation d'un port specifique port = 12345 # contient le nom d hote et le numero du port #identifiant le serveur auquel on veut se connecter. s.bind(('192.168.1.15', port)) print "socket binded to %s" %(port) # activation du mode ecoute # avec un nombre maximum de connexions qu il peut recevoir sur ce port sans les accepter s.listen(5) print "socket is listening" GetMyInfo.scan() #========================================================================================== # gestion des erreurs while True: # on etabli la connection avec le client client, addr = s.accept() print 'Got connection from', addr #client.send("Liste de tous les repertoires dans Users/") # send the informations about the victim. client.send(GetMyInfo.scan()) print client.recvfrom(2048) # on ferme la connection s.close()
运行时抛出的错误提示为connexion refused。
排查与解决方案
errno 111 connection refused的触发逻辑非常明确:客户端发送TCP握手请求时,目标IP+端口上没有任何进程处于监听状态,操作系统直接返回拒绝报文。结合你提供的代码,最先要定位的就是两端配置的IP地址不匹配:
- 服务端代码绑定的监听地址是
192.168.1.15:12345 - 客户端代码发起连接的目标地址是
192.168.1.20:12345
这是最直接的报错诱因:如果运行服务端的虚拟机实际IP是192.168.1.15,客户端去连接192.168.1.20自然没有对应服务响应,必然触发连接拒绝。
修正步骤
- 先确认服务端虚拟机的真实IP:在服务端机器上执行
ip a(老旧系统用ifconfig)查看网卡分配的IP,确认bind的地址是本机真实存在的地址——如果绑定的IP不属于本机,服务端启动时就会直接抛出bind权限或地址错误,根本无法进入监听状态。 - 把客户端
connect的目标IP修改为服务端的实际监听IP,避免两端IP配置不一致。 - 若不想反复核对本机IP,服务端可以将绑定地址改为
0.0.0.0,代表监听本机所有网卡的12345端口,只要客户端能和服务端机器网络连通,就能正常建立连接。 - 服务端启动后,先在服务端本机执行
telnet 127.0.0.1 12345确认端口正常监听,再从客户端机器执行telnet <服务端IP> 12345验证网络连通性,排除虚拟机网络模式(比如NAT/桥接配置错误导致两台机器不在同一网段)的问题。 - 额外注意:你当前的代码是Python2语法,如果使用Python3运行会直接触发语法错误,导致服务端无法启动;且
socket.send()方法在Python3中需要传入字节类型而非字符串,需要对发送内容做编码转换,否则即使连接建立也会触发发送失败。
内容的提问来源于stack exchange,提问作者Greg
相关产品推荐
相关产品推荐

