Python套接字监听失败,目标机主动拒绝



我已经通过

运行了这个python脚本
python myproxy.py

,得到以下错误:

Traceback (most recent call last):
File "proxy.py", line 4, in <module>
client_socket.connect(('localhost', 5000))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

脚本:

import socket, time
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 5000))
serversocket.listen(20) # become a server socket, maximum 20 connections
while True:
connection, address = serversocket.accept()
data = client_socket.recv(512)
print("RECEIVED: %s" % data)
client_socket.send(data)

有很多stackoverflow的答案讨论这是一个防火墙问题。我已经创建了一个允许端口5000的传入和传出防火墙规则,但没有工作。请不要把这些问题联系起来,不像以前的问题,让我们避免让彼此失望。

我不是telnet专家,但是我试了一下:

Telnet

Welcome to Microsoft Telnet Client
Escape Character is ']'
Microsoft Telnet> open localhost 5000
Connecting To localhost...Could not open connection to the host, on port 5000: Connect failed
Microsoft Telnet> open 127.0.0.1 5000
Connecting To 127.0.0.1...Could not open connection to the host, on port 5000: Connect failed
Microsoft Telnet>

我也在使用代理。我关闭了代理,这并没有解决这个问题。一般来说,我更喜欢在使用代理时使用这个脚本,但如果没有其他选择,关闭代理也是可以的。

我该如何解决这个问题?

更新:

使用

client_socket.connect((socket.gethostbyname(socket.gethostname()), 5000))

不太有用:

client_socket.connect((socket.gethostbyname(socket.gethostname()), 5000))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

尝试用socket.gethostbyname(socket.gethostname())替换'localhost'
您还需要serversocket在服务器脚本中

您可以尝试使用客户端的ip地址更改'localhost'。

我有几乎相同的问题,直到我在客户端(在我的情况下是WinSCP)用我的ip地址替换主机名(localhost)。

特别是本地循环(127.0.0.1或localhost)不能与机器的显式ip地址互换。

相关内容

最新更新