修改过后的高速率发包工具

Zss 发表于:

接上之前发包工具的修改,主要修改速率小于32Kpkt/s时的问题

速率现在为:90Kpkt/s和15pkt/s左右

记录一下,记录一下。。。

#coding:utf-8
from socket import *
from threading import Thread
import time

def Broadcast():
    if speed == '1':
        while 1:
            udpsocket.sendto('',('192.168.99.255',8888))
    if speed == '2':
        while 1:
            udpsocket.sendto('',('192.168.99.255',8888))
            time.sleep(0.001)

def Multicast():
    if speed == '1':
        while 1:
            udpsocket.sendto('',('224.0.1.0',8888))
    if speed == '2':
        while 1:
            udpsocket.sendto('',('224.0.1.0',8888))
            time.sleep(0.001)

def Unicast(ip):
    if speed == '1':
        while 1:
            udpsocket.sendto('',(ip,8888))
    if speed == '2':
        while 1:
            udpsocket.sendto('',(ip,8888))
            time.sleep(0.001)


def send(task):
    T_list = []
    if task == 'Unicast':
       print('-'*30)
        ip = raw_input('输入AP的ip:')
        print('正在发包中...')
        if speed == '1':number = 3
        if speed == '2':number = 30
        for i in range(number):
            T = Thread(target=Unicast,args=(ip,))
            T.start()
            T_list.append(T)

    if task == 'Multicast':
       print('-'*30)
        print('正在发包中...')
        if speed == '1':number = 3
        if speed == '2':number = 30
        for i in range(number):
            T = Thread(target=Multicast)
            T.start()
            T_list.append(T)

    if task == 'Broadcast':
       print('-'*30)
        print('正在发包中...')
        if speed == '1':number = 3
        if speed == '2':number = 30
        for i in range(number):
            T = Thread(target=Broadcast)
            T.start()
            T_list.append(T)

    for a in T_list:
        a.join()


def input_choice():
    print('-' * 30)
    print('    单播,广播,组播发包工具')
    print('PS:\n1.单播请设置一个存在的IP,例如:AP的ip\n2.>32Kpkt/s约为90Kpkt/s左右\n3.<32Kpkt/s约为15Kpkt/s左右\n4.速度具体根据电脑CPU性能相关\n5.程序死循环发包,结束请手动关闭\n6.不能开启两个程序一起发,会占用相同的端口')
    print('-'*30)
    print('IP为: %s'%localip)
    print('端口为: 7777')
    print("数据包每秒增量可以复制工具目录下的Count cmd.txt中的命令")
    with open('Count cmd.txt','w') as f:
        f.write("while :;do a=`cat /proc/net/dev|grep eth0|awk '{print $2}'`;sleep 1;b=`cat /proc/net/dev|grep eth0|awk '{print $2}'`;echo eth0:$((b-a));done")
    print('-'*30)
    print('(1).发送广播')
    print('(2).发送组播')
    print('(3).发送单播')
    print('-'*30)
    choice = raw_input('输入你的选择:')
    return choice

def input_speed():
    print('-'*30)
    print('(1).速率 > 32Kpkt/s')
    print('(2).速率 < 32Kpkt/s')
    print('-'*30)
    choice = raw_input('输入你的选择:')
    return choice


if __name__ == '__main__':
    localip = gethostbyname(gethostname())
    udpsocket = socket(AF_INET,SOCK_DGRAM)
    udpsocket.bind(('',7777))
    choice = input_choice()
    speed = input_speed()
    if choice == '1':
        send('Broadcast')
    elif choice == '2':
        send('Multicast')
    elif choice == '3':
        send('Unicast')
    else:pass
    raw_input()