简体   繁体   English

即使在我完全关闭防火墙后,防火墙也不允许我的 python 客户端应用程序连接到我的机器上运行的服务器

[英]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:这是我到目前为止所尝试的:

  1. 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这没有用

  2. 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 上运行的服务

  3. 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 版本

  4. 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.

相关问题 Python http.如果我不关闭服务器将无法工作 Windows 防火墙 - Python http.server won't work if i dont turn off Windows Firewall 无法建立连接,因为目标机器主动拒绝它——我如何检查我的服务器是否正在运行并且客户端是否能够连接 - No connection could be made because the target machine actively refused it— How can i check if my server is running and client is able to connect PHP / Python检查防火墙是否关闭 - PHP / Python Check if firewall is turned off Python客户端拒绝连接到我的SocketServer服务器 - Python client refuses to connect to my SocketServer server 如何使用 Python 中的套接字库将我的服务器连接到其他机器? - How to connect my server to other machine using socket library in Python? Web应用防火墙开发 - web application firewall development 我需要在防火墙中允许什么协议才能从 python 连接到 apache spark - what protocol to I need to allow in firewall in order to connect from python to apache spark 当我打开我的电脑时运行 python 脚本 - running a python script when i turn my pc on 端口 8000 即使在防火墙上启用后也不会启用 Ubuntu 18 - port 8000 not enable even after enable it on firewall Ubuntu 18 运行python时如何永久接受OSX防火墙允许/拒绝确认? - How can I permanently accept OSX firewall allow/deny confirmation when running python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM