Netstat Speed

Jorgen Grahn grahn+nntp at snipabacken.dyndns.org
Sat Sep 2 13:49:17 EDT 2006


On Sat, 2 Sep 2006 13:22:59 +0200, Sybren Stuvel <sybrenUSE at YOURthirdtower.com.imagination> wrote:
...
> def activeip(checkip):
>      # if there is any line returned we set activeb to 1 indicating that
>      # this ip is active during a netstat run   
>      activeb = 0          
>      netstat = os.popen("netstat -a -n | grep '%s'" % checkip)
>      for line in netstat:
>          s = line
>          if s:
>             activeb = 1
>             break
>      netstat.close()
>      return activeb 
>
> Another way would be to let grep do the work for you. Pass it the '-q'
> option and check its exit code.

I suspect it's faster to skip the grep altogether and do the string search
in Python. That also means you can drop the implicit shell invocation, and
just exec netstat, period.

No matter what, I think you'll find most of the wall time is spent starting
netstat and having it wade through kernel statistics.

/Jorgen

-- 
  // Jorgen Grahn <grahn@        Ph'nglui mglw'nafh Cthulhu
\X/     snipabacken.dyndns.org>  R'lyeh wgah'nagl fhtagn!



More information about the Python-list mailing list