简体   繁体   English

当我连接到 vps 时出现错误:连接被拒绝

[英]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.

相关问题 Errno 61:尝试连接到python服务器时出现连接拒绝错误 - Errno 61: Connection refused error when trying to connect to python server python套接字上的连接被拒绝(端口已打开,我可以与nc连接) - Connection refused on python sockets (port is open, I can connect with nc) 使用python suds连接到Microsoft CRM时,为什么会出现连接错误/超时? - Why do I get a connection error / timeout when using python suds to connect to Microsoft CRM? 如何修复“连接到 localhost:6379 的错误 111。 当我启动 rq-worker 时连接被拒绝? - How to fix 'Error 111 connecting to localhost:6379. Connection refused' when I start rq-worker? 在Heroku上安装Celery时出现“ [[Errno 111]连接被拒绝”错误 - Get “[Errno 111] Connection refused” error when mount Celery on Heroku I / O错误(套接字错误):[Errno 111]连接被拒绝 - I/O error(socket error): [Errno 111] Connection refused 是什么导致连接错误 111 拒绝连接 - What is causing connection error 111 refused to connect 尝试连接到蓝牙服务器时连接被拒绝 - Connection refused when trying to connect to bluetooth server 如何以正确的方式捕获连接拒绝错误? - How can I catch a connection refused error in a proper way? 为什么我在 Python 中收到错误“连接被拒绝”? (插座) - Why am I getting the error “connection refused” in Python? (Sockets)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM