Netstat Speed

Tal Einat tal.no.no.spam at gmail.com
Sat Sep 2 07:59:48 EDT 2006


DarkBlue wrote:
> Following code works .
> My question is can I make it faster ?
>
Faster how? Are you calling this so many times that you need it to be
extremely fast? Are you calling it on the same IP multiple times, on
many different IPs at once, ...?

>
> 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
>      for line in os.popen("netstat -a -n | grep '%s'" % checkip) :
>      s=line
>      if s <>'' :
>         activeb=1
>      return activeb
>

Since you state you want this faster I assume you're calling this quite
a bit. To start with, perhaps you could save the output of netstat, go
over it once collecting IPs, and save these for future lookup. Surely
this will be faster than running a shell command every time.

- Tal




More information about the Python-list mailing list