473,573 Members | 3,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Netstat Speed


Following code works .
My question is can I make it faster ?
def activeip(checki p):
# 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("netst at -a -n | grep '%s'" % checkip) :
s=line
if s <>'' :
activeb=1
return activeb
Thanks
Sep 2 '06 #1
6 3091
s=line
is actually indented even if it does not appear to be so :)

Db
Sep 2 '06 #2

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(checki p):
# 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("netst at -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

Sep 2 '06 #3
Sybren Stuvel wrote:
DarkBlue enlightened us with:
>Following code works .

No it doesn't - it's indented badly. I guess you mean something like:

def activeip(checki p):
# 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("netst at -a -n | grep '%s'" % checkip) :
s=line
if s <>'' :
activeb=1
return activeb

Also try to use != instead of <>. Being equal or not has nothing to do
with being larger or smaller.
>My question is can I make it faster ?

As soon as you set activeb=1, it no longer changes to any other value.
That means that you could immediately return from that point. It's
also cleaner to actually close an open file:

def activeip(checki p):
# 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("netst at -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.

Sybren
Thank you for pointing that out.
Speed did not improve , but it definitely
is cleaner code now.

Have a nice day
Db

Sep 2 '06 #4
On Sat, 2 Sep 2006 13:22:59 +0200, Sybren Stuvel <sy*******@YOUR thirdtower.com. imaginationwrot e:
....
def activeip(checki p):
# 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("netst at -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.dyn dns.org R'lyeh wgah'nagl fhtagn!
Sep 2 '06 #5
On Sat, 2 Sep 2006 13:22:59 +0200, Sybren Stuvel <sy*******@YOUR thirdtower.com. imaginationwrot e:
DarkBlue enlightened us with:
>Following code works .

No it doesn't - it's indented badly. I guess you mean something like:

def activeip(checki p):
# 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("netst at -a -n | grep '%s'" % checkip) :
s=line
if s <>'' :
activeb=1
return activeb

Also try to use != instead of <>. Being equal or not has nothing to do
with being larger or smaller.
Yeah, that took me a few seconds to mentally convert to !=. Personally, I'd
just write "if line:" in this case.

While we're at it, please write the documentation from a /user/ standpoint,
not as internal scribblings. And don't call the address 'checkip' -- that
makes it look like you pass a functor to the function. Maybe something like
this:

| def ip_is_active(ad dr):
| """Return success if 'addr' shows up in the output from 'netstat -an'.
| We assume that a suitable version of netstat exists.
| """
| activeb=0
| ...

This doc string is vague -- but that's because it's unclear what the
function does and what you expect it to do. I see half a dozen problems or
so, but that might be ok if its callers don't expect too much.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyn dns.org R'lyeh wgah'nagl fhtagn!
Sep 2 '06 #6
On Sat, 2 Sep 2006 20:13:01 +0200, Sybren Stuvel <sy*******@YOUR thirdtower.com. imaginationwrot e:
Jorgen Grahn enlightened us with:
>>| def ip_is_active(ad dr):
| """Return success if 'addr' shows up in the output from 'netstat -an'.
| We assume that a suitable version of netstat exists.
| """

If I have an "if" in the docstring, I always write the "else" case as
well:

"""Returns True if 'addr' shows up in the output from 'netstat -an',
and False otherwise. We assume that a suitable version of netstat
exists.
"""

This makes it clear what happens in either case.
Tastes differ. I think it's clear enough without the "else". Note that I
changed the name of the function too, to make it read like a test (or rather
an assertion): "if not ip_is_active('1 27.0.0.1')".

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyn dns.org R'lyeh wgah'nagl fhtagn!
Sep 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
23032
by: Yang Li Ke | last post by:
Hi guys, Is it possible to know the internet speed of the visitors with php? Thanx -- Yang
34
2448
by: Jacek Generowicz | last post by:
I have a program in which I make very good use of a memoizer: def memoize(callable): cache = {} def proxy(*args): try: return cache except KeyError: return cache.setdefault(args, callable(*args)) return proxy which, is functionally equivalent to
7
3039
by: YAZ | last post by:
Hello, I have a dll which do some number crunching. Performances (execution speed) are very important in my application. I use VC6 to compile the DLL. A friend of mine told me that in Visual studio 2003 .net optimization were enhanced and that i must gain in performance if I switch to VS 2003 or intel compiler. So I send him the project and...
6
2019
by: Ham | last post by:
Yeah, Gotto work with my VB.Net graphic application for days, do any possible type of code optimization, check for unhandled errors and finally come up with sth that can't process 2D graphics and photos at an acceptable speed. I have heard things about the virtual machine of Mr. Net, that it can run my app at a high speed....but could never...
1
2991
by: Agnes | last post by:
in form_load, i will new a sql connection ,fill in the dataset and then close the connection. I use Netstat to check the status. (1)Open the form , The status to the SQL server is "ESTABILISHED" (2)Close the form, The status is "TIME_WAIT" (3)WAIT a sec. that thread is disappear . my question is , I had closed the connection after fill in...
10
4802
by: billie | last post by:
Hi all. I don't know if Python is good for this kind of jobs but I'm wondering if it's possible emulate the "netstat" command in Python. I'd need to know if a certain executable opened a socket and, in that case, I'd like to know what kind of socket it uses (TCP or UDP), its src/dst PORT, and the current STATE of the connection (listening,...
6
6235
by: Jassim Rahma | last post by:
I want to detect the internet speed using C# to show the user on what speed he's connecting to internet?
11
6478
by: kyosohma | last post by:
Hi, We use a script here at work that runs whenever someone logs into their machine that logs various bits of information to a database. One of those bits is the CPU's model and speed. While this works in 95% of the time, we have some fringe cases where the only thing returned is the processor name. We use this data to help us decide which...
4
8601
by: nestle | last post by:
I have DSL with a download speed of 32MB/s and an upload speed of 8MB/s(according to my ISP), and I am using a router. My upload speed is always between 8MB/s and 9MB/s(which is above the max upload speed), ALWAYS. However, my download speed doesn't go over 25MB/s. And when my brother turns on the internet from his computer and takes up half the...
0
7778
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8017
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8201
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7777
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5585
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5290
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2203
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1300
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1039
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.