Hi everyone, I wanna ask about Bytes from ifconfig eth0.
Like us know, menu's from ifconfig eth0 is...

eth0 Link encap:Ethernet HWaddr 00:0F:20:CF:8B:42
inet addr:217.149.127.10 Bcast:217.149.127.63 Mask:255.255.255.192
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2472694671 errors:1 dropped:0 overruns:0 frame:0
TX packets:44641779 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1761467179 (1679.7 Mb) TX bytes:2870928587 (2737.9 Mb)
Interrupt:28

And the question is, how to make RX bytes, and TX bytes into kbps?
If we running the script, will be like this...

$speed.py eth0
Upspeed: 150 Kb/s
Downspeed: 32 Kb/s

Sorry, if my question make a little confusing. because I'm still practice learning python...

RX bytes and TX bytes are not speed measurements, they show the total number of bytes received and transmitted by this interface (eth0).

Are you a friend of zero_1 who posted the same question 1 week ago Click Here ?

No, thats me, im forget email password. So i make the new one. And change the question to be converting speed Bytes from eth0
to kbps for linux and not windows anymore

I don't understand your question because kbps is kilobits per second, a speed measurement, but ifconfig doesn't give any speed indication.

yeah me too, i change the os to linux for this practice from my lecture. I tried to make another script.
But this script can't made like the sample before...

import time
import os
import sys

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

if __name__ == '__main__':
    (tx_prev, rx_prev) = (0, 0)
    while(True):
        tx = get_bytes('tx')
        rx = get_bytes('rx')
        if tx_prev > 0:
            tx_speed = tx - tx_prev
            print('TX: ', tx_speed, 'bps')
        if rx_prev > 0:
            rx_speed = rx - rx_prev
            print('RX: ', rx_speed, 'bps')
        time.sleep(1)
        tx_prev = tx
        rx_prev = rx

Note:
Sample is like i written above...
speed.py eth0
upspeed: 150 kb/s
downspeed: 30 kb/s

If you only want to add an interface argument to your script, use the argparse module like this

import argparse
import time
import os
import sys

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

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="estimates internet interface speed")
    parser.add_argument('iface', metavar='INTERFACE', help='network interface such as eth0 or wlan0')
    args = parser.parse_args()
    (tx_prev, rx_prev) = (0, 0)
    while(True):
        try:
            tx = get_bytes('tx', args.iface)
            rx = get_bytes('rx', args.iface)
        except IOError:
            print("Cannot read the number of bytes received or transmitted through interface {}".format(args.iface))
            break
        if tx_prev > 0:
            tx_speed = tx - tx_prev
            print('TX: ', tx_speed, 'bps')
        if rx_prev > 0:
            rx_speed = rx - rx_prev
            print('RX: ', rx_speed, 'bps')
        time.sleep(1)
        tx_prev = tx
        rx_prev = rx

Also it seems to me that using

import psutil
d = psutil.net_io_counters(pernic=True)

is a more robust way to obtain the numerical data (most probably cross-platform).

It's good option using import argparse Now, i will try to make this script like the sample

Problem Solved, Here's The Code...

#!/usr/bin/python3


import argparse
import time
import os
import sys

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

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="estimates internet interface speed")
    parser.add_argument('iface', metavar='INTERFACE', help='network interface such as eth0 or wlan0')
    args = parser.parse_args()
    (tx_prev, rx_prev) = (0, 0)
    while(True):
        try:
            tx = get_bytes('tx', args.iface)
            rx = get_bytes('rx', args.iface)
        except IOError:
            print("Cannot read the number of bytes received or transmitted through interface {}".format(args.iface))
            break
        if tx_prev > 0:
            tx_speed = tx - tx_prev
            print('Down: ', tx_speed, 'Kbps')
        if rx_prev > 0:
            rx_speed = rx - rx_prev
            print('Up: ', rx_speed, 'Kbps')
        time.sleep(1)
        tx_prev = tx
        rx_prev = rx

Tx = Download Speed, and Rx = Upload Speed...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.