networking - Detect if network is idle in Python -


i making python based downloader. have working multiprocessing based console script of now. know how detect if network idle. is, users not using network (browsing, surfing, etc).

it should able 2 things in regard:

  1. resume downloading when network detected idle.
  2. pause downloading when detects network activity.

one way define 'idle-ness' trigger if network activity @ 1% of max bandwidth 5 minutes straight.

is there better way detect if network idle?

i recommend using psutil.network_io_counters module:

return network i/o statistics namedtuple including 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 dropped
  • dropout: total number of outgoing packets dropped (always 0 on osx , bsd)

if pernic true return same information every network interface installed on system dictionary network interface names keys , namedtuple described above 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 sent , received bytes on interface want monitor , give information if network idle or busy.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -