[英]Firewall is not allowing my python client application to connect to a server running on my machine even after I turn off firewall completely
I am learning about the python socket library and am running into problems whenever I try to connect to the server running on my localhost with a client application.我正在学习 python 套接字库,每当我尝试使用客户端应用程序连接到在我的本地主机上运行的服务器时都会遇到问题。
Here is the server code:这是服务器代码:
import socket
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
print(data)
Here is the code for my client application:这是我的客户端应用程序的代码:
import socket
HOST = "localhost" # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
Here is my error message:这是我的错误信息:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Here is what I have tried so far:这是我到目前为止所尝试的:
Disabling my Window's 10 firewall completely on the windows command prompt with the use of the following command: netsh advfirewall set allprofiles state off.使用以下命令在 windows 命令提示符下完全禁用我的 Window 10 防火墙:netsh advfirewall set allprofiles state off。 This did not work
这没有用
To windows firewall I added an inward rule and outward rule that allows any application on my OS to access a service running on port 65432在 windows 防火墙中,我添加了一个内向规则和外向规则,允许我的操作系统上的任何应用程序访问在端口 65432 上运行的服务
I changed my python version from 3.8.2 to 3.7.7 because before hand I was able to run this code perfectly and I was using a python 3.7 version我将我的 python 版本从 3.8.2 更改为 3.7.7,因为之前我能够完美地运行此代码并且我使用的是 python 3.7 版本
I tried multiple different methods of setting the HOST variable, which include "localhost", '127.0.0.1', socket.gethost(), and socket.gethostbyname("localhost")我尝试了多种不同的设置 HOST 变量的方法,包括“localhost”、“127.0.0.1”、socket.gethost() 和 socket.gethostbyname(“localhost”)
I am able to connect to the server through the use of the Window's telnet application but that is it.我可以通过使用 Window 的 telnet 应用程序连接到服务器,但仅此而已。 To be honest I have exhausted possible solutions that I can find online, and I know that this question has came up on this website a lot, but I have honestly tried every solution I have seen so far - which included three hours of searching.
老实说,我已经用尽了我可以在网上找到的可能的解决方案,而且我知道这个问题在这个网站上出现了很多,但我诚实地尝试了迄今为止我看到的每一个解决方案——其中包括三个小时的搜索。
I appreciate any possible help that you guys can give, thanks.我感谢你们可以提供的任何可能的帮助,谢谢。
Since the code was working earlier in the machine,this doesn't seem to be code issue.由于代码在机器中较早地工作,这似乎不是代码问题。 Also the code ran fine in my machine.
代码在我的机器上也运行良好。 I suggest you to run through the below steps once again:
我建议您再次执行以下步骤:
Solution 1:解决方案1:
1. edit the server address as 127.0.0.1 or the host private IP in both the code just to be assured there is no discrepancy. 1. 在两个代码中将服务器地址编辑为 127.0.0.1 或主机私有 IP 以确保没有差异。
2. Start the server program first and make sure it didn't terminate. 2.首先启动服务器程序并确保它没有终止。
3. Start the client application and check if the server program threw any error or exceptions. 3. 启动客户端应用程序并检查服务器程序是否抛出任何错误或异常。
Solution 2: Change the port number and follow solution 1.解决方案 2:更改端口号并按照解决方案 1。
Solution 3: Switch off the windows firewall from the UI just to be sure.解决方案 3:从 UI 中关闭 windows 防火墙以确保安全。 Follow the solution 1 steps
按照解决方案 1 步骤
Solution 4: Change the server address as host=''解决方案4:将服务器地址更改为host=''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.