How can I calculate internet data usage in python ? I tried psutil but i am not really sure how to calculate it is it write to use psutil and can we use socket or scapy ?
-
Can you please explain in more detail what you want to do? For example - do you want to know how many bytes were sent and recieved by each interface? Is this for a specific OS?Guy– Guy2022-08-12 18:45:04 +00:00Commented Aug 12, 2022 at 18:45
-
Yes I want to know that and also to calculate the internet usage for my device like when windows calculate itEsra Issam– Esra Issam2022-08-12 18:48:37 +00:00Commented Aug 12, 2022 at 18:48
-
If you are using linux, then this post - stackoverflow.com/questions/1052589/… is for you. A quick internet search find this github package - gist.github.com/racerxdl/d4b4670d189ad579ae1aGuy– Guy2022-08-12 18:49:07 +00:00Commented Aug 12, 2022 at 18:49
-
no actually I'm using windowsEsra Issam– Esra Issam2022-08-12 18:50:21 +00:00Commented Aug 12, 2022 at 18:50
-
but with python is it different ?Esra Issam– Esra Issam2022-08-12 18:50:36 +00:00Commented Aug 12, 2022 at 18:50
|
Show 1 more comment
1 Answer
If I understood the question correctly, then the following code will give you what you need-
>>> import psutil
>>> psutil.net_io_counters(pernic=True)
{'lo': snetio(bytes_sent=547971, bytes_recv=547971, packets_sent=5075, packets_recv=5075, errin=0, errout=0, dropin=0, dropout=0),
'wlan0': snetio(bytes_sent=13921765, bytes_recv=62162574, packets_sent=79097, packets_recv=89648, errin=0, errout=0, dropin=0, dropout=0)}
This is copied straight from the docs
The result is the amount of data sent and recieved for each interface. You will have to determine which interface you are interested in, perhaps the interface relevant to you Wifi. What you will probably be most interested in are the bytes_sent and bytes_recv values.
1 Comment
Esra Issam
with this way can I measure the internet usage ?