简体   繁体   English

Python套接字可通过LAN工作,但不能通过Wifi工作

[英]Python socket works over LAN but not over Wifi

I have a simple UDP server implemented in python: 我有一个在python中实现的简单UDP服务器:

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("",10005))

while True:
    data = sock.recv(1024)

I run this code on computer A. I send UDP commands from computer B in these two situations: 我在计算机A上运行此代码。在这两种情况下,我从计算机B发送UDP命令:

  1. Both A and B are connected to a router in a local network via LAN cable. A和B都通过LAN电缆连接到本地网络中的路由器。
  2. Both A and B are connected to router over Wifi. A和B都通过Wifi连接到路由器。

The UDP packets are received in situaltion 1 (LAN Cable) but not in situation 2 (over Wifi). UDP数据包在情况1(LAN电缆)中接收,但在情况2(通过Wifi)中不接收。 In both the cases Wireshark shows the received packet on computer A. Any thoughts? 在这两种情况下,Wireshark在计算机A上显示收到的数据包。有什么想法?

OS: Windows 操作系统:Windows

The client program: 客户端程序:

import socket 
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(char,("192.168.1.107",10005))
sock.close()

I have come close to finding the solution. 我已接近找到解决方案。 Windows is dropping the UDP packets. Windows正在丢弃UDP数据包。 I checked with netstat -s -p UDP command. 我检查了netstat -s -p UDP命令。 Whenever the sending computer sends the UDP packets, the Receive Errors increase. 每当发送计算机发送UDP数据包时,接收错误就会增加。 Now I just have to figure out why the packets are being received erroneously. 现在我只需弄清楚错误地接收数据包的原因。

Edit I have tested it on other computers. 编辑我已在其他计算机上测试过它。 It works. 有用。 I have switched of the firewall on the computer where it doesn't work but still can not figure out what is filtering out the UDP packet. 我已经在计算机上切换了防火墙,但它无法正常工作,但仍然无法弄清楚什么是过滤掉UDP数据包。

Check the trust setting on the Wifi network for the server machine. 检查服务器计算机的Wifi网络上的信任设置。 According to this article from Microsoft: 根据微软的这篇文章

For example, a program that accepts inbound connections from the Internet (like a file sharing program) may not work in the Public profile because the Windows Firewall default setting will block all inbound connections to programs that are not on the list of allowed programs. 例如,接受来自Internet的入站连接的程序(如文件共享程序)可能无法在公共配置文件中工作,因为Windows防火墙默认设置将阻止所有入站连接到不在允许程序列表中的程序。

I believe by default Wifi networks are put in the Public profile, so it sounds like what's happening here. 我相信默认情况下,Wifi网络被置于公共配置文件中,所以听起来就像这里发生的一样。 Since you know the packet is getting there OK (form wireshark), the most likely explanation is that the firewall refuses to deliver it for you. 既然你知道数据包正好(形成wireshark),最可能的解释是防火墙拒绝为你提供它。

The alternative would be to add python to the allowed programs list if you are perhaps not wholly trusting of the network. 如果您可能不完全信任网络,则可以选择将python添加到允许的程序列表中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM