Scapy的嗅探()函数不工作没有明显的原因
问题描述:
我试图使用sniff()
功能Scapy的规定,但它提出了以下错误:Scapy的嗅探()函数不工作没有明显的原因
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 *
可以单独的'getLen()'问题的一部分?在'getMessage'中,'l = getLen()'可能甚至会打印'l'而不是调用'sniff(count = l,lfilter = filterFrom)',因为调用你的代码时getLen()不会提出问题,我们不能使用getLen因为它使用现有的套接字。 – DorElias