user1310420
user1310420

Reputation:

Python network bandwidth monitor

I am developing a program in python, and one element tells the user how much bandwidth they have used since the program has opened (not just within the program, but regular web browsing while the program has been opened). The output should be displayed in GTK

Is there anything in existence, if not can you point me in the right direction. It seems like i would have to edit an existing proxy script like pythonproxy, but i can't see how i would use it.

Thanks,

Upvotes: 9

Views: 36378

Answers (5)

Kris Stern
Kris Stern

Reputation: 1340

Would something like WireShark (https://wiki.wireshark.org/FrontPage) do the trick? I am tackling a similar problem now, and am inclined to use pyshark, a WireShark/TShark wrapper, for the task. That way you can get capture file info readily.

Upvotes: 0

Piotr Jasiek
Piotr Jasiek

Reputation: 61

import time

def get_bytes(t, iface='wlan0'):
    with open('/sys/class/net/' + iface + '/statistics/' + t + '_bytes', 'r') as f:
        data = f.read();
        return int(data)

while(True):
    tx1 = get_bytes('tx')
    rx1 = get_bytes('rx')

    time.sleep(1)

    tx2 = get_bytes('tx')
    rx2 = get_bytes('rx')

    tx_speed = round((tx2 - tx1)/1000000.0, 4)
    rx_speed = round((rx2 - rx1)/1000000.0, 4)

    print("TX: %fMbps  RX: %fMbps") % (tx_speed, rx_speed)

should be work

Upvotes: 6

Jimilian
Jimilian

Reputation: 3919

For my task I wrote very simple solution using psutil:

import time
import psutil

def main():
    old_value = 0    

    while True:
        new_value = psutil.net_io_counters().bytes_sent + psutil.net_io_counters().bytes_recv

        if old_value:
            send_stat(new_value - old_value)

        old_value = new_value

        time.sleep(1)

def convert_to_gbit(value):
    return value/1024./1024./1024.*8

def send_stat(value):
    print ("%0.3f" % convert_to_gbit(value))

main()

Upvotes: 17

Amyth
Amyth

Reputation: 32949

Well, not quiet sure if there is something in existence (written in python) but you may want to have a look at the following.

  1. Bandwidth Monitoring (Not really an active project but may give you an idea).

  2. Munin Monitoring (A pearl based Network Monitoring Project)

  3. ntop (written in C/C++, based on libpcap)

Also just to give you pointers if you are looking to do something on your own, one way could be to count and store packets using sudo cat /proc/net/dev

Upvotes: 3

Joe
Joe

Reputation: 47609

A proxy would only cover network applications that were configured to use it. You could set, e.g. a web browser to use a proxy, but what happens when your proxy exits?

I think the best thing to do is to hook in lower down the stack. There is a program that does this already, iftop. http://en.wikipedia.org/wiki/Iftop

You could start by reading the source code of iftop, perhaps wrap that into a Python C extension. Or rewrite iftop to log data to disk and read it from Python.

Upvotes: 0

Related Questions