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>What is your worry so far?  YOu have a certain amount of innate overhead, since you have to return to the start of the file.  We can reduce it somewhat by getting rid of the superfluous <strong>open</strong> commands:</p>

<pre><code>import time
old = 0
with open('/sys/class/net/wlan1/statistics/tx_bytes') as vol:
    while True:
        vol.seek(0)
        new = vol.read()
        time.sleep(1)
        print "UP:", new-old
        old = new
</code></pre>

<p>The <strong>seek</strong> command resets the file's "bookmark"" to byte 0, the start of the file.  It's faster than reopening the file, since it's merely an integer assignment to an object attribute.</p>

<p>Is this granularity good enough for your purposes?</p>

python time ifconfig