Scapy sniff() 函数无缘无故不起作用

Scapy sniff() function not working for no apparent reason

我正在尝试使用 scapy 提供的 sniff() 函数,但它引发了以下错误:

Traceback (most recent call last):

  File "TestCode.py", line 54, in <module>
    packets = getMessege()

  File "TestCode.py", line 45, in getMessege
    return sniff(count=getLen(), lfilter=filterFrom)

  File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\site-packages\scapy\sendrecv.py", line 575, in sniff

    sel = select([s],[],[],remain)

select.error: (10038, 'An operation was attempted on something that is not a socket')

这是代码(FromGlobal 是一个包含发件人 IP 和端口的元组):

def getLen():
    while True:
        length, LenFrom = sock.recvfrom(1024)
        try:
            IntLen = int(length)
        except:
            pass
        else:
            if LenFrom == FromGlobal:
                return IntLen

def filterFrom(pck):
    try:
        return pck[IP].src == FromGlobal[0] and pck[UDP].sport == FromGlobal[1]
    except:
        return False

def getMessege():  # TODO This needs to return only the messege and port
    return sniff(count=getLen(), lfilter=filterFrom)

packets = getMessege()
print packets.show()

奇怪的是,如果我尝试这样做:

def func1():
    return int('1')

def lfilter(pack):
    return TCP in pack and pack[IP].src != '8.8.8.8'

def func():
    return sniff(count=func1(), lfilter=lfilter)

var = func()
print var.show()

它工作得很好。如果有人能指出两者之间的区别,那将会有很大帮助。

我正在使用 WinPcap 4.1.3 和 scapy 2.x。

嗯,自己解决了。显然,如果你这样做:

from scapy.all import *
from scapy.layers.inet import *

嗅探功能不起作用,所以只能

from scapy.all import *