Scapy sniff() function not working for no apparent reason

时间:2016-12-26 16:30:45

标签: python python-2.7 scapy sniffing

I'm trying to use the sniff() function that scapy provides but it raises the following error:

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')

Here is the code (FromGlobal is a tuple that contains the IP and Port of the sender):

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()

The weird part is that if I try to do it like so:

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()

it works perfectly well. If someone could point out the difference between the two it would help a lot.

I'm use WinPcap 4.1.3 and scapy 2.x.

1 个答案:

答案 0 :(得分:0)

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

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

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

from scapy.all import *