Yes.
Using the sniff()
function, you can provide a parameter to the stop_filter
option.
>>> print sniff.__doc__
Sniff packets
sniff([count=0,] [prn=None,] [store=1,] [offline=None,] [lfilter=None,] +
L2ListenSocket args) -> list of packets
...clipped...
stop_filter: python function applied to each packet to determine
if we have to stop the capture after this packet
ex: stop_filter = lambda x: x.haslayer(TCP)
If the function returns 1, sniff will stop, and you can continue with whatever logic you wish.