Take main () and get_network_bytes () and wrap them in a python process:
import subprocess import time from daemon import runner import datetime class App(): def __init__(self): self.stdin_path = '/dev/null' self.stdout_path = '/dev/tty' self.stderr_path = '/dev/tty' self.pidfile_path = '/tmp/YOUR_PROCESS_NAME.pid' self.pidfile_timeout = 5 def run(self): counter1 = 0 counter_log = 0 try: while True:
Then start python with myscript.py or stop or restart, the process ID will be printed on the screen. In the background, you will need to import this script as part of your other program to request it, or you can always print the result in a temporary file each time it loops, and then use your other program) to request this temporary file. "There are several different ways to do this."
Edit:
Start with something similar and configure, this is really the only way to do this, I would suggest a different library like psutil, rather than a messy ifconfig parsing that only works on linux:
"Remember to change the interface, I use WLAN0, you can use something else"
The following script will display the average TX and RX values ββfor the last minute every 60 seconds.
import os import subprocess import time from daemon import runner import datetime import re class App(): def __init__(self): self.stdin_path = '/dev/null' self.stdout_path = '/dev/tty' self.stderr_path = '/dev/tty' self.pidfile_path = '/tmp/bandwidth_counter.pid' self.pidfile_timeout = 5 def run(self): rx_megabits_new = 0 tx_megabits_new = 0 try: while True: output = subprocess.Popen(['ifconfig', "wlan0"], stdout=subprocess.PIPE).communicate()[0] rx_bytes = re.findall('RX bytes:([0-9]*) ', output)[0] tx_bytes = re.findall('TX bytes:([0-9]*) ', output)[0] rx_megabits = (((int(rx_bytes) * 8) / 1024) / 1024) tx_megabits = (((int(tx_bytes) * 8) / 1024) / 1024) current_rx_usage = rx_megabits - rx_megabits_new current_tx_usage = tx_megabits - tx_megabits_new rx_megabits_new = rx_megabits tx_megabits_new = tx_megabits print 'average megabits received', current_rx_usage / 60 print 'average kilobits received', (current_rx_usage * 1024) / 60 print 'average megabits sent', current_tx_usage / 60 print 'average kilobits sent', (current_tx_usage * 1024) / 60 time.sleep(60) except Exception, e: raise app = App() daemon_runner = runner.DaemonRunner(app) daemon_runner.do_action()
Again this works with: "python myscript.py start" and stops and restarts. You want to add the file open and write, and then print on the screen. I also suggest that you open files for writing with gzip.open () to save some space, as they will increase rapidly.
Edit:
Here is the same daemon, this time recording the file / tmp / netstats _counter.log, which is a csv containing "tx speed in KB / s for 1 minute intervals, average speed rx in KB / s for 1- minute intervals, unix timestamp "
import os import subprocess import time from daemon import runner import datetime import re class App(): def __init__(self): self.stdin_path = '/dev/null' self.stdout_path = '/dev/tty' self.stderr_path = '/dev/tty' self.pidfile_path = '/tmp/twitter_counter.pid' self.pidfile_timeout = 5 def run(self): rx_megabits_new = 0 tx_megabits_new = 0 try: while True: output_csv = open("/tmp/netstats_counter.log", 'a') output = subprocess.Popen(['ifconfig', "wlan0"], stdout=subprocess.PIPE).communicate()[0] rx_bytes = re.findall('RX bytes:([0-9]*) ', output)[0] tx_bytes = re.findall('TX bytes:([0-9]*) ', output)[0] rx_megabits = (((int(rx_bytes) * 8) / 1024) / 1024) tx_megabits = (((int(tx_bytes) * 8) / 1024) / 1024) current_rx_usage = (rx_megabits - rx_megabits_new) / 60 current_tx_usage = (tx_megabits - tx_megabits_new) / 60 current_rx_usage_kb = current_rx_usage * 1024 current_tx_usage_kb = current_tx_usage * 1024 rx_megabits_new = rx_megabits tx_megabits_new = tx_megabits now = str(datetime.datetime.now()) col1 = str(current_tx_usage_kb) col2 = str(current_rx_usage_kb) output_csv.write(col1) output_csv.write(", ") output_csv.write(col2) output_csv.write(", ") output_csv.write(now) output_csv.write("\n") output_csv.close( )