I want to run 2 blocking loops in the same program. In my program, I'm using nfqueue to intercept packets. When the queue is created it starts waiting for packets and blocks the program. When a packet arrives it will call the cb() function and then start listening again for a new packet.
Here is my program:
import nfqueue, socket
from scapy.all import *
import os
os.system('iptables -t mangle -A PREROUTING -j NFQUEUE --queue-num 1')
os.system('iptables -t mangle -A POSTROUTING -j NFQUEUE --queue-num 2')
count = 0
def cb(payload):
global count
count +=1
data = payload.get_data()
p = IP(data)
print str(count) + ": TOS = " + str(p.tos)
payload.set_verdict(nfqueue.NF_ACCEPT)
def run_queue(queue_num):
print "Preparing the queue"
q = nfqueue.queue()
q.open()
q.unbind(socket.AF_INET)
q.bind(socket.AF_INET)
q.set_callback(cb)
q.create_queue(queue_num)
try:
print "Running the queue"
q.try_run()
except KeyboardInterrupt, e:
print "interruption"
q.unbind(socket.AF_INET)
q.close()
run_queue(1)
run_queue(2)
How can I run 2 or more of these blocking loops in the same program?
Any help would be very appreciated. Thank you!