Python 如何停止计时器线程
这是我使用线程的代码Python 如何停止计时器线程,python,scapy,traffic,packets,Python,Scapy,Traffic,Packets,这是我使用线程的代码 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):
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()
我想在一段时间后停止线程,因为在这段代码中,直到我发出键盘中断才会停止。
我该怎么做呢?线程是坚不可摧的,哈哈哈。改用多处理,这样你就可以把它们全部杀掉 没有
多处理计时器
,因此只需在运行进程之前使用导入时间
时间。睡眠(2)
t=threading.Timer(2,send\u p,())
变为t=multiprocessing.Process(target=send\u p)
使用您的代码的完整示例:
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
返回有关每次运行的信息的代码块
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)))
>线程不能被销毁、停止、挂起、恢复或中断我想捕获每秒捕获的字节数,并发送不同的流量模式和捕获它们的速率。我应该做什么?啊,是的,通过线程,它将打印到同一个屏幕上,通过多处理,你必须将其传递回主进程以显示它。我已经更新了我的答案,包括我将如何做到这一点。是的,这就是最后一行将显示的内容,如果需要,您可以在while循环中删除之前的打印内容,并且只显示平均每秒字节数。它将您在
send_p()
中生成的所有速率收集到列表routes
中,并将所有速率平均化。基本上,我会在这一点上进行负载测试吗?简单的选择:1。阻塞直到P2P完成,或2。为P2P计时,并在x秒后将其杀死。选项1,其中显示time.sleep(100)
只需将代码/函数调用添加到P2P流量生成器。选项2,将代码创建为一个单独的函数,并对其执行另一个多端口调用,然后在其后面添加time.sleep()
。我更新了答案,以显示将在何处包括P2P流量开始。但我真的不知道如何产生它,这是它自己的问题。祝你的项目好运!