Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am making a Python based downloader. I have a working multiprocessing based console script as of now. I would like to know how to detect if the network is idle. That is, the users not using the network themselves (browsing, surfing, etc).

It should be able to do two things in this regard:

  1. Resume downloading when network is detected idle.
  2. Pause downloading when it detects some network activity.

One way to define 'idle-ness' could be to trigger if the network activity is at 1% of max bandwidth for 5 minutes straight.

Is there a better way to detect if network is idle?

share|improve this question
ifconfig eth0 which would give you rx and tx bytes and if run at regular intervals could give you a simple metric. I would wrap the command in pexpect (or similar) and examine the output. However I too would be interested in a proper way to do this. – Verma Aug 1 at 3:43
The issue with this approach is that I can't know which process is taking how much network. I would like to know this to figure out if a browser or other action is causing network traffic and not my own downloader. – MyPreciousss Aug 7 at 22:12
@MyPreciousss That is a really difficult application to develop, I have done that before but you have to access each operating system's kernel and retrieve network stats per process. I will get into specifics later on when I post my answer. – enginefree yesterday

This question has an open bounty worth +50 reputation from MyPreciousss ending in 1 hour.

This question has not received enough attention.

Please provide a way to implement a silent updater (eg. Windows Update)

2 Answers

I'm kind of wondering if this is the right approach at all. You will have to average over some time and check very often, and you will still have TOCTTOU problems (That is, just at exactly the same time you decide to start your next download segment, the user clicks on something that starts network activity).

A better approach may actually be to simply throttle (to a user-configurable level?) the download bandwidth of your application, so it only downloads at X KB/s. Yes, it will be a bit slower, but it will be much easier to implement, and much easier to understand how it works.

share|improve this answer

I would recommend using the psutil.network_io_counters module:

Return network I/O statistics as a namedtuple including the following attributes:

  • bytes_sent: number of bytes sent
  • bytes_recv: number of bytes received
  • packets_sent: number of packets sent
  • packets_recv: number of packets received
  • errin: total number of errors while receiving
  • errout: total number of errors while sending
  • dropin: total number of incoming packets which were dropped
  • dropout: total number of outgoing packets which were dropped (always 0 on OSX and BSD)

If pernic is True return the same information for every network interface installed on the system as a dictionary with network interface names as the keys and the namedtuple described above as the values.

In [1]: import psutil
In [2]: psutil.network_io_counters(pernic=True)
Out[2]:
{'en0': iostat(bytes_sent=143391435L, bytes_recv=1541801914L,
               packets_sent=827983L, packets_recv=1234558L, errin=0L, errout=0L,
               dropin=0L, dropout=0),
 'gif0': iostat(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L,
                errin=0L, errout=0L, dropin=0L, dropout=0),
 'lo0': iostat(bytes_sent=6143860L, bytes_recv=6143860L, packets_sent=55671L,
               packets_recv=55671L, errin=0L, errout=0L, dropin=0L, dropout=0),
 'p2p0': iostat(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L,
                errin=0L, errout=0L, dropin=0L, dropout=0),
 'stf0': iostat(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L,
                errin=0L, errout=0L, dropin=0L, dropout=0)
}

You can keep track of the sent and received bytes on the interface you want to monitor and that will give you information if the network is idle or busy.

share|improve this answer
How can I detect which process is taking how much? To be truly non-intrusive to the user's network use, I need to be able to detect if the bandwidth is from some other process or my own downloader. – MyPreciousss Aug 9 at 17:05
On linux systems you can check the /proc/PID/net/dev and you also have a bwmon. I know that mac has nettop, but I don't know how to do this on a BSD system. – Viktor Kerkez Aug 9 at 22:56
Or you could use iptables to mark the packages depending on the source/port/destination etc. – Torxed yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.