0

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 ?

6
  • 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? Commented 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 it Commented 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/d4b4670d189ad579ae1a Commented Aug 12, 2022 at 18:49
  • no actually I'm using windows Commented Aug 12, 2022 at 18:50
  • but with python is it different ? Commented Aug 12, 2022 at 18:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

with this way can I measure the internet usage ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.