Python Errno 10061:无法建立连接,因为目标机器主动拒绝它(客户端-服务器)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12993276/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:21:29  来源:igfitidea点击:

Errno 10061 : No connection could be made because the target machine actively refused it ( client - server )

pythonsockets

提问by havox

I have a problem with these client and server codes, I keep getting the [Errno 10061] No connection could be made because the target machine actively refused it

我的这些客户端和服务器代码有问题,我不断收到[Errno 10061] 由于目标机器主动拒绝,无法建立连接

I'm running the server on a virtual machine with Windows XP SP3 and the client on Windows 7 64bit, my python version is 2.7.3. What I want to know is how should I edit the code to use the client and server on different networks! Thanks!

我在带有 Windows XP SP3 的虚拟机和 Windows 7 64 位客户端上运行服务器,我的 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 : code is from internet.

PS:代码来自互联网。

回答by user207421

10061 is WSAECONNREFUSED, 'connection refused', which means there was nothing listening at the IP:port you tried to connect to.

10061 是 WSAECONNREFUSED,“连接被拒绝”,这意味着在您尝试连接的 IP:port 上没有任何监听。

There was a firewall product around the year 2000 that issued refusals instead of ignoring incoming connections to blocked ports, but this was quickly recognised as an information leak to attackers and corrected or withdrawn.

大约在 2000 年左右,有一款防火墙产品会发出拒绝消息,而不是忽略到被阻止端口的传入连接,但这很快就被认为是向攻击者泄露了信息,并予以纠正或撤回。

回答by purza95

Using the examples from: https://docs.python.org/3.2/library/socketserver.htmlI determined that I needed to set the HOST port to the machine I had the server program running on. So TCPServer on 192.168.0.1 HOST = TCPServer IP 192.168.0.1 then I had to set the TCPClient side to point to the TCPServer IP. So the TCPClient HOST value = 192.168.0.1 - Sorry, that's the best I can describe it.

使用以下示例:https: //docs.python.org/3.2/library/socketserver.html我确定我需要将 HOST 端口设置为运行服务器程序的机器。所以 TCPServer on 192.168.0.1 HOST = TCPServer IP 192.168.0.1 然后我必须设置 TCPClient 端指向 TCPServer IP。所以 TCPClient HOST 值 = 192.168.0.1 - 对不起,这是我能描述的最好的。

回答by Frank Nocke

Hint: actively refusedsounds like somewhat deeper technical trouble, but...

提示:actively refused听起来像是更深层次的技术问题,但是......

...actually, this response (and also specifically errno:10061) is also given, if one calls the bin/mongo executable and the mongodb service is simply not runningon the target machine. This even applies to local machine instances (all happening on localhost).

...实际上,errno:10061如果调用 bin/mongo 可执行文件并且 mongodb 服务根本没有在目标机器上运行,也会给出这个响应(并且特别是)。这甚至适用于本地机器实例(都发生在本地主机上)。

? Always rule out for this trivial possibility first, i.e. simply by using the command line client to access your db.

? 首先总是排除这种微不足道的可能性,即简单地使用命令行客户端访问您的数据库。

See here.

看这里。

回答by M.Selmy

The solution is to use the same IP and Port number in both client and server. Try, in client to use TCP_IP = 'write the ip number here' TCP_PORT = writ the port number here s.connect((TCP_IP, TCP_PORT))

解决方案是在客户端和服务器中使用相同的 IP 和端口号。试试,在客户端使用 TCP_IP = 'write the ip number here' TCP_PORT = writ the port number here s.connect((TCP_IP, TCP_PORT))

回答by Zzmilanzz Zzmadubashazz

if you have remote server installed on you machine. give server.py host as "localhost" and the port number. then client side , you have to give local ip- 127.0.0.1 and port number. then its works

如果您的机器上安装了远程服务器。将 server.py 主机作为“localhost”和端口号。然后是客户端,您必须提供本地 ip-127.0.0.1 和端口号。那么它的作品

回答by Karisno1

the short term solution is to use the default iis host and port normally 120.0.0.1 and 80 respectively. However am still looking for a more versatile solution.

短期解决方案是使用默认的 iis 主机和端口,通常分别为 120.0.0.1 和 80。但是,我仍在寻找更通用的解决方案。

回答by Vinaykumar Patel

I was facing a similar problem when I was calling REST API using python library and what I found that my server was going into sleep mode which was leading to this. As soon as I logged in to the server via Remote Desktop Connection, my API call used to work.

当我使用 python 库调用 REST API 时,我遇到了类似的问题,我发现我的服务器正在进入睡眠模式,这导致了这个问题。一旦我通过远程桌面连接登录到服务器,我的 API 调用就可以正常工作。

回答by Alex

This could be because of proxy or firewall. If it's proxy, then you need to specify proxy setting at entry point of your code or project.

这可能是因为代理或防火墙。如果是代理,则需要在代码或项目的入口点指定代理设置。

import os #for proxy

proxy = 'http://10.XX.XX.XX:8X8X' #your own proxy 'http://<user>:<pass>@<proxy>:<port>'

os.environ['http_proxy'] = proxy 
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy
#rest of code .....

回答by only4

When you run the code on windows machine, firewall prompts it to allow network access, allow the network access and it will work, if it does not prompts, go to firewall settings > allow an app through firewall and select your python.exe and allow network access.

当你在windows机器上运行代码时,防火墙提示它允许网络访问,允许网络访问它会工作,如果没有提示,去防火墙设置>允许应用程序通过防火墙并选择你的python.exe并允许网络访问。

回答by Libross

The below changes fixed my problem. I struggled with the same error for a week. I would like to share with you all that the solution is simply host = ''in the server and the client host = ipof the server.??

以下更改解决了我的问题。我为同样的错误苦苦挣扎了一个星期。我想和大家分享一下,解决方案只是host = ''在服务器和服务器的客户端host = ip。??