How to calculate data transfer ifconfig per second in python?
<p>I have script like this</p>

<pre><code>import os
import time
os.chdir('/sys/class/net/wlan1/statistics')

while True:
    up = open('tx_bytes').read()
    time.sleep(1)
    print 'UP:',up 
</code></pre>

<p>And the value is</p>

<pre><code>UP: 2177453

UP: 2177539

UP: 2177789

UP: 2177990
</code></pre>

<p>How to find diferent value TX bytes after delay time.sleep and Befor delay time.sleep to get bandwidth Byte persecond?
Byte persecond = TXbytes_after_time_sleep - TXbytes_before_time_sleep
and the value can loopen no endless with while true.</p>

<p>Just like you did I guess.</p>

<p>It's quite rough and not very accurate but the same time spent reading the before is taken into account in reading the after so I guess it's fine.</p>

<pre><code>with open('/sys/class/net/wlan1/statistics/tx_bytes') as myfile:
    before = myfile.read()
time.sleep(3)  # The longer this is the more accurate it will be on average.
with open('/sys/class/net/wlan1/statistics/tx_bytes') as myfile:
    after = myfile.read()
print((int(after)-int(before))/3)
</code></pre>

python time ifconfig