首页 > 解决方案 > Windows 10 上的 Python 套接字问题

问题描述

我正在尝试在 Windows 10 上捕获数据包。我使用了官方文档页面的说明:

import socket

# the public network interface
HOST = socket.gethostbyname(socket.gethostname())

# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))

# Include IP headers
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

# receive all packages
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

# receive a package
while True:
    print(s.recvfrom(65565))    

# disabled promiscuous mode
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

以前我能够捕捉到它们,但现在我只看到一些虚拟的 UDP 数据包

标签: pythonwindowssocketsnetworkingsniffer

解决方案


Found the root cause: socket.gethostbyname() returns IP of Ethernet adapter(I don't use such, but it become active after system update). So disabling it fix the issue.


推荐阅读