2016-12-26 96 views
0

我試圖使用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.

+0

可以單獨的'getLen()'問題的一部分?在'getMessage'中,'l = getLen()'可能甚至會打印'l'而不是調用'sniff(count = l,lfilter = filterFrom)',因爲調用你的代碼時getLen()不會提出問題,我們不能使用getLen因爲它使用現有的套接字。 – DorElias

回答

0

恩,自己解決吧。顯然,如果你這樣做:

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

了呼吸功能將無法工作這麼只做

from scapy.all import *