Source code for gwa.eperf.function

#!/usr/bin/pytho
#-*-coding:iso-8859-15-*-

import time,commands,re



[docs]def helpEperf(): """ This function allow to display the Eperf Help. usage :: eperf -s [option] eperf [-net <host>] [-UDP|-TCP] [-upload|-download] [option] eperf [-h|--help] Server specific ( 'eperf -s' ) :: -s Run in server mode (mandatory) -p Server port to listen on (default TCP : 5000 / UDP : 5001)(optional) Client specific :: -net <host> Run in client mode, connecting to <host> (mandatory) -UDP Client mode UDP connecting to <host> (mandatory) -TCP Client mode TCP connecting to <host> (mandatory) [-upload|-download] Client mode upload or download or both (one is mandatory) -p Server port to connect to (default TCP : 5000 / UDP : 5001)(optional) -i <interval> Seconds between periodic bandwidth reports (default 1 secs)(optional) -d <duration> Time in seconds to transmit for (default 5 secs)(optional) -c <connection> For TCP mode, the number of connection simultanouse (default 3 connection)(optional) -bw <bandwidth> For UDP, bandwidth to send at in bits/sec (default 50 Mbits/s)(optional) """ print """usage: eperf -s [option] eperf [-net <host>] [-UDP|-TCP] [-upload|-download] [option] eperf [-h|--help] Server specific ( 'eperf -s' ) : -s Run in server mode (mandatory) -p Server port to listen on (default TCP : 5000 / UDP : 5001)(optional) Client specific : -net <host> Run in client mode, connecting to <host> (mandatory) -UDP Client mode UDP connecting to <host> (mandatory) -TCP Client mode TCP connecting to <host> (mandatory) [-upload|-download] Client mode upload or download or both (one is mandatory) -p Server port to connect to (default TCP : 5000 / UDP : 5001)(optional) -i <interval> Seconds between periodic bandwidth reports (default 1 secs)(optional) -d <duration> Time in seconds to transmit for (default 5 secs)(optional) -c <connection> For TCP mode, the number of connection simultanouse (default 3 connection)(optional) -bw <bandwidth> For UDP, bandwidth to send at in bits/sec (default 50 Mbits/s)(optional) """
[docs]class Timer: """ This function, we allow to create a timer reference. """ def __init__(self): y = (1970, 1, 1, 1, 0, 0, 0, 0, 0) self.y = time.mktime(y)
[docs] def restart(self): """ This function, we allow to reset a timer reference. """ self.y = time.time()
[docs] def get_value(self): """ This function, we allow to give a timer. """ x = time.time() result = x-self.y return result
[docs]def strNbData(nbBytes): """ This function convert and return a string with the rigth unit of the data. :Argument: nbBytes (type float) This argument represents the number of bytes. nbsec (type float) This argument represents the number of second. :Return: A String with the bandwidth. **example** >>> strbandwidth(800,1) '6.250 Kbits/s' >>> strbandwidth(100000,5) '156.250 Kbits/s' """ if float(nbBytes) >= (1024*1024*1024) : return "%.3f GBytes"%(float(nbBytes)/(1024*1024*1024)) if float(nbBytes) >= (1024*1024) : return "%.3f MBytes"%(float(nbBytes)/(1024*1024)) if float(nbBytes) >= (1024) : return "%.3f KBytes"%(float(nbBytes)/(1024)) else : return "%.3f Bytes"%(float(nbBytes))
[docs]def strbandwidth(nbBytes,nbsec): """ This function calculate and return a string with the rigth unit of the bandwidth. :Argument: nbBytes (type float) This argument represents the number of bytes. nbsec (type float) This argument represents the number of second. :Return: A String with the bandwidth. **example** >>> strbandwidth(800,1) '6.250 Kbits/s' >>> strbandwidth(100000,5) '156.250 Kbits/s' """ if nbsec== 0: return -1 test=float(nbBytes)*8/nbsec if test >= 1024*1024*1024 : return "%.3f Gbits/s"%( ( (float(nbBytes)*8) / (1024*1024*1024) ) / nbsec ) if test >= 1024*1024 : return "%.3f Mbits/s"%( ( (float(nbBytes)*8) / (1024*1024) ) / nbsec ) if test >= 1024 : return "%.3f Kbits/s"%( ( (float(nbBytes)*8) / (1024) ) / nbsec ) else : return "%.3f bits/s"%( (float(nbBytes)*8) / nbsec )
[docs]def getIpAddr(): """ This function allow to read the IP Address on all network interfaces. :Return: A tuples list with name of interface and this IP Address. [ ('name_of_interface', 'IP_address'),('lo', '127.0.0.1')] **example** >>> IP=getIpAddr() >>> IP [('eth0', '192.168.1.37'), ('lo', '127.0.0.1'), ('wlan0', '192.168.1.22')] """ res=[] output=commands.getoutput("ifconfig") output=[i for i in output.split("\n")] for i in range(len(output)): if re.search("lo",output[i]) and re.search("addr:",output[i+1]): res=res+[("lo",output[i+1].split("addr:")[1].split(" ")[0])] if re.search("eth0",output[i]) and re.search("addr:",output[i+1]): res=res+[("eth0",output[i+1].split("addr:")[1].split(" ")[0])] if re.search("wlan0",output[i])and re.search("addr:",output[i+1]): res=res+[("wlan0",output[i+1].split("addr:")[1].split(" ")[0])] return res