[英]When I connect to vps I get an error: Connection refused
I wrote in vps- console two files, that work great (test message comes from the client and is displayed by the server script).我在 vps-console 中写了两个文件,效果很好(测试消息来自客户端并由服务器脚本显示)。 Server.py:服务器.py:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
sock.bind(('localhost', 8884))
while True:
try:
client, addr = sock.accept()
except KeyboardInterrupt:
sock.close()
break
else:
client.setblocking(True)
result = client.recv(1024)
client.close()
print('Message', result.decode('utf-8'))
Clien.py:客户端.py:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 8884))
sock.send(b'Test message!')
sock.close()
But if I use Client.py from my home computer , I get an error:但是,如果我在家用计算机上使用 Client.py,则会收到错误消息:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('145.148.81.8', 8884)) #ConnectionRefusedError: [Errno 111] Connection refused
sock.send(b'Test message from my house!')
sock.close()
How to fix?怎么修?
your server code:您的服务器代码:
sock.bind(('localhost', 8884))
means that the server is only listening for incoming connections on loopback device.表示服务器仅在环回设备上侦听传入连接。
Change that localhost to 0.0.0.0
and then the server listens on all available network devices.将该 localhost 更改为0.0.0.0
,然后服务器侦听所有可用的网络设备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.