简体   繁体   English

sendto()python

[英]sendto() python

I use python program to do traffic generator in Linux Ubuntu, and the code like below: 我使用python程序在Linux Ubuntu中做流量生成器,代码如下:

import socket, sys

host = sys.argv[1] #Server IP Address
textport = sys.argv[2] #Server Binding Port

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #socket

try:

    port = int(textport)

except ValueError:

    port = socket.getservbyname(textport, 'udp')

while 1:

   try:
        data = open('auth3.log')#read file

        for each_line in data: #each rows
                try:
                        (role,line_spoken) = each_line.split(': ',1)#split two parts
                        role  =  role.strip()
                        s.sendto(role, (host, port))
                        print('Send: ' + str(role) + "\n" )
                except:
                        pass

    except IOError as err:
        print('file isn\'t exist!!~'+str(err))

    finally:
        if 'data' in locals():   #If data have no object, Don't use data to close!!~
                data.close()

    print "\n"

The size of auth3.log is about 1.8M. auth3.log的大小约为1.8M。

When I send data to the destination server, I use snmp which OID is ''ifInOctets'' to get the traffic information. 当我将数据发送到目标服务器时,我使用snmp,其中OID是''ifInOctets''以获取流量信息。

But I the traffic recalculate to unit of ''Kbits'' is about 128. 但我重新计算到''Kbits''的流量大约是128。

How can I use this program to fill the bandwidth up to 1Gbits?(In other words, I want to fill out the bandwidth) 如何使用此程序将带宽填充到1Gbits?(换句话说,我想填写带宽)

Thanks for your helping. 谢谢你的帮助。

Your program is not running fast enough to generate 1Gbps on the wire. 您的程序运行速度不够快,无法在线路上生成1Gbps。

To make it run faster, you can: 为了使其运行更快,您可以:

  1. Remove the call to print after sendto . 取下调用printsendto (Print is slow by nature.) (打印本质上很慢。)
  2. Preprocess your auth3.log file so that you do not need to process it within your inner loop. 预处理您的auth3.log文件,这样您就不需要在内部循环中处理它。 (Right now you are looping on .split and .strip , both of which are wasting CPU time. (现在你正在循环.split.strip ,这两者都在浪费CPU时间。
  3. Rewrite your program to send larger chunks. 重写您的程序以发送更大的块。

But, I fear the result will still not reach 1Gbps. 但是,我担心结果仍然不会达到1Gbps。 To really max out your line, try using a traffic generation program such as Colasoft Packet Builder (although I'm not sure even that program will do it. 1Gbps is a lot of traffic.) 为了真正最大化您的线路,尝试使用流量生成程序,如Colaso​​ft Packet Builder (虽然我不确定程序是否会这样做.1Gbps是一个很大的流量。)

This version of your code implements the first two optimizations suggested by Chris Merck. 此版本的代码实现了Chris Merck建议的前两个优化。

import socket, sys, itertools

host = sys.argv[1] #Server IP Address
textport = sys.argv[2] #Server Binding Port

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

try:
    port = int(textport)
except ValueError:
    port = socket.getservbyname(textport, 'udp')

# preprocess file data    
with open('auth3.log') as data:
    roles = [role for (role, line_spoken) in line.split(': ', 1) for line in data]

# preprocess everything we can
hp = (host, port)
send = s.sendto

for role in itertools.cycle(roles):
    try:
        send(role, hp)
    except:
        pass

For further optimizations, you might want to process it using Cython , which might further speed up the loop. 为了进一步优化,您可能希望使用Cython处理它,这可能会进一步加快循环。 If the code still doesn't generate enough traffic, you'll probably need to launch several processes in parallel. 如果代码仍然没有产生足够的流量,您可能需要并行启动多个进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM