This is my code in which I am using a thread
from scapy.all import *
from random import randint
import threading
# Generate packet
pkts = IP(src="10.0.0.1",dst="10.0.0.2")/TCP()/Raw(RandString(size=120))
#print pkts
pkts[TCP].flags = "UFP"
pktList = []
for pktNum in range(0,5):
pktList.extend(pkts)
pktList[pktNum][TCP].dport = randint(1,65535) # Pkt has Ran PortNo.
print pktList[pktNum].summary()
#print len(pktList[pktNum])
wrpcap('tcp-packets.pcap',pktList[pktNum])
# Send the list of packets send(pktList)
def send_p():
start_time=time.time()
send(pktList)
totalTime = time.time()-start_time
print totalTime,"seconds"
totalBytes=(50*120)/totalTime
print '%4.1f' % totalBytes, "B/s"
t=threading.Timer(2,send_p,())
t.start()
t=threading.Timer(2,send_p,())
t.start()
I want to stop thread after some interval of time as in this code doesn't stops until I issue a keyboard interrupt. How would I do that?
Threads are indestructible, BuwaHahaha. Use multiprocessing instead, that way you can KILL THEM ALL.
There isn't a multiprocessing.Timer
, so instead just use import time
time.sleep(2)
before running the process.
t=threading.Timer(2,send_p,())
becomes t=multiprocessing.Process(target=send_p)
Full example using your code:
import multiprocessing
import time
# Code that you want to execute then later execute
time.sleep(2)
t=multiprocessing.Process(target=send_p)
t.start()
time.sleep(100) # "Some interval of time"
if t.is_alive():
t.terminate() # Kill it with fire! or SIGTERM on linux
Code block returning information about each run.
from scapy.all import *
from random import randint
import threading
import time
import multiprocessing
from itertools import count
# Generate packet
pkts = IP(src="10.0.0.1",dst="10.0.0.2")/TCP()/Raw(RandString(size=120))
#print pkts
pkts[TCP].flags = "UFP"
pktList = []
for pktNum in range(0,5):
pktList.extend(pkts)
pktList[pktNum][TCP].dport = randint(1,65535) # Pkt has Ran PortNo.
print pktList[pktNum].summary()
#print len(pktList[pktNum])
wrpcap('tcp-packets.pcap',pktList[pktNum])
# Send the list of packets send(pktList)
def send_p(queue):
for run_number in count(): # this will run indefinitely, same as while True, must be killed to stop.
start_time=time.time()
send(pktList)
totalTime = time.time()-start_time
totalBytes=(50*120)/totalTime
queue.put((run_number, totalTime, totalBytes))
def create_p2p_traffic():
pass # do stuff
q = multiprocessing.Queue()
t = multiprocessing.Process(target=send_p, args=(q, ))
t.start()
time.sleep(10)
### Runs without P2P traffic
rates = []
while True: # This loop will pull all items out of the queue and display them.
run = q.get()
if not run: # When we reach the end of the queue, exit
break
print "Average rate of {0:.1f} B/s without p2p traffic".format(sum(rates)/float(len(rates)))
### Runs with P2P traffic
p = multiprocessing.Process(target=create_p2p_traffic)
p.start()
time.sleep(10) # "Some interval of time"
if t.is_alive():
t.terminate()
if p.is_alive():
p.terminate()
p2prates = []
while True: # This loop will pull all items out of the queue and display them.
run = q.get()
if not run: # When we reach the end of the queue, exit
break
run_number, total_time, total_bytes = run
print "Run {run_number} took a total of {total_time}\
at an average rate of {total_bytes:.1f} B/s".format(run_number=run_number,
total_time=total_time,
total_bytes=total_bytes)
p2prates.append(total_bytes)
print "Average rate of {0:.1f} B/s after p2p started".format(sum(p2prates)/float(len(p2prates)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.