Python 无法建立连接,因为目标机器主动拒绝它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4532335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No connection could be made because the target machine actively refused it
提问by user551717
I've tried to connect to my local machine every time I try and run my program. I am a nub, so it's probably a simple mistake somewhere.
每次尝试运行我的程序时,我都尝试连接到本地机器。我是一个小人,所以这可能是某个地方的一个简单错误。
def connect(self):
self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.conn.connect((self.host,self.port))
That is the code causing the error. The host and port are defined. Why is it giving me this error report?
那是导致错误的代码。定义了主机和端口。为什么给我这个错误报告?
[Errno 10061] No connection could be made because the target machine actively refused it
[Errno 10061] 由于目标机器主动拒绝,无法建立连接
采纳答案by dutt
It's because you haven't opened the port you are trying to connect to, nothing is listening there. If you're trying to connect to a web or ftp server, start it first. If you're trying to connect to another port, you need to write a server application too.
这是因为您还没有打开您尝试连接的端口,那里没有任何东西在监听。如果您尝试连接到 Web 或 ftp 服务器,请先启动它。如果您尝试连接到另一个端口,则还需要编写一个服务器应用程序。

