Cannot trace error in python pcapy wrapper
the answer is pretty simple:p.next()
will throw
on timeout
your timeout is 100ms
(last parameter of open_live
)
so your except
should handle the timeout case and you may want to increase the timeout
time or set it to 0
for infinite
edit:
you simply expected socket.timeout
but PcapError
is thrown instead. socket.timeout
is the exception thrown by the socket code in the python lib so it is python specific. It is getting wrapped up (maybe just with new versions of pcapy) or it jsut stands for a different kind of timeout (TCP socket related)
see example pcapy code: example
pcapy
doesn't use Python socket
module. It won't raise socket.timeout
which is raised if timeout has been enabled by previous socket.settimeout
call. socket.settimeout
is used to set a socket
into blocking, non-blocking or timeout state.
In pcapy
, the timeout argument of open_live
is passed to poll
syscall at least in Linux, should differ by OS where poll
is not available.
Reader.next
call raises PcapError
if there's no packet to return because it hasn't captured any packets yet. It's not an error, just an indication like StopIteration
. It can be ignored and Reader.next
has to be called again.
Reader.loop
won't return until it has at least one packet to return or an error occurs.
The following code captures 10 packets and exits.
from pcapy import open_live, findalldevs, PcapErrorp = open_live("eth0", 1024, False, 100)dumper = p.dump_open("test.pcap")devices = findalldevs()print dumper, devicescount=0while True: try: packet = p.next() except PcapError: continue else: print packet count += 1 if count == 10: break