all ip addresses of machines in the local network

S

Simon Forman

Chaz said:
I was writing some code that used someone else class as a subclass. He
wrote me to tell me that using his class as a subclass was incorrect. I
am wondering under what conditions, if ever, does a class using a
subclass not work.

Here is an example. For instance the original class might look like:

class A :
def __init__(self,arg) :
self.foo = arg
def bar(self) :
return self.foo


And I defined a class B1 which looked like:


class B1(A);
def __init__(self,a1,a2) :
self.c = a1
A.__init__(self,ag)


He said I should use it this way:

class B2:
def __init__(self,a1,a2):
self.c = a1
self.t = A(a2)

def bar(self) :
self.t.bar()


Other than the obvious difference of B2 having an attribute 't', I can't
see any other obvious differences. Is there something I am missing?

TIA
Chaz

When the developer *tells* you it won't work, that's a good indication.
:)

You haven't missed anything: the developer was talking about his
specific code, not python in general. (I'm on the Twisted list too.
;-) )

Peace,
~Simon
 
G

Gabriel Genellina

That is merely a logical use of OO after all when would a car and an
orange be the same?

Uh... what's the point...?
By example, an orange inside a car would be modeled using
composition, never inheritance.
I was wondering more about the mechanics of Python: when does B1 show
different characteristics than B2 (forgoing the obvious simple things,
like 't' above).

Inheritance implies that *all* methods/attributes of A are exposed by
B1; it's directly supported by the language. Inheritance is usually a
relationship between classes. If you add a method foo() to A,
instances of B1 automatically have it. A B1 instance "is an" A instance.
Using delegation, you have to delegate the desired method calls
yourself (but there are ways to do that automatically, too).
Delegation is a relationship between instances. If you add a method
foo() to A, you have to add it to B2 too. A B2 instance "is not an" A instance.



Gabriel Genellina
Softlab SRL



p4.vert.ukl.yahoo.com uncompressed Thu Aug 24 21:27:05 GMT 2006


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
R

Rick Zantow

Uh... what's the point...?
By example, an orange inside a car would be modeled using
composition, never inheritance.

I've heard of cars that seem to inherit from the *lemon* class, though. Not
a good object model, that.
 
G

Gabriel Genellina

Once again you answered all the generic things about classes. I
could have taken that from a book on OO. All well and good but not
specifically addressed to the question I asked. Please read what I
wrote. I am more interested in knowing specifics about the Python
implementation and if there are any "gotchas" that would make B1
different from B2.

Please stay on the list.

b1 = B1()
b2 = B2()
isinstance(b1, A) -> True
isinstance(b2, A) -> False

For any other method defined in A, say foo:
b1.foo() is OK
b2.foo() raises AttributeError

So you decide to override foo() too (in both implementations, B1 and B2)
Let's say, A.bar() calls self.foo()
b1.bar() calls B1.foo on b1
b2.bar() calls A.foo on t - B2.foo is *not* called.

Enough examples? Inheritance and delegation are *not* the same thing.
Anyway, none of these examples is very Python-specific.



Gabriel Genellina
Softlab SRL



p5.vert.ukl.yahoo.com uncompressed Fri Aug 25 01:27:04 GMT 2006


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
O

Ognjen B

Amit said:
I expect that you would know the IP range for your network. Then you
can simply 'ping' each IP in the range to find wether its alive.
Moreover by your description I guess you would actually want to find
all machines in your network that run a particular network service, to
allow you to "distribute the database". In such case you can use
"nmap" with -p option, to find all the machines which are listening on
the particular port.

hth,
amit.
Correct me if I am wrong, but isn't a way of doing this to use ARP?
(Address Resolution protocol, see
http://en.wikipedia.org/wiki/Address_Resolution_Protocol ) send an ARP
request, and wait for the reply. For example:

you have a network, 192.168.5.0, with a netmask of 255.255.255.0 This
means you have 254 addresses, so with ARP, it would go somthing like this:

your program >> "Who has 192.168.5.1"

and if anyone has the IP, they go "Hey, I (hw MAC address) have IP"

Do this with the entire range (192.168.5.1 --> 192.168.5.254) and you
get a list of the devices IP addresses and corresponding MAC addresses.
This is how some network scanners I use work, to build a list of
connected systems.

Or you can use one of the other programs out there ,like nmap or nast,
as they will output this list and you can parse it to your hearts content.

Of course, anyone feel free to correct me if I made a mistake, its been
a while since I last did this.
 
D

David Ells

Chaz said:
I was writing some code that used someone else class as a subclass. He
wrote me to tell me that using his class as a subclass was incorrect. I
am wondering under what conditions, if ever, does a class using a
subclass not work.

Here is an example. For instance the original class might look like:

class A :
def __init__(self,arg) :
self.foo = arg
def bar(self) :
return self.foo


And I defined a class B1 which looked like:


class B1(A);
def __init__(self,a1,a2) :
self.c = a1
A.__init__(self,ag)


He said I should use it this way:

class B2:
def __init__(self,a1,a2):
self.c = a1
self.t = A(a2)

def bar(self) :
self.t.bar()


Other than the obvious difference of B2 having an attribute 't', I can't
see any other obvious differences. Is there something I am missing?

TIA
Chaz

This is also known as White Box inheritance vs. Black Box inheritance,
or inheritance vs. composition, although it doesn't necessarily have
the same full implications in Python (since private variables and
methods of a class can still be accessed without much trouble, in any
code, not just the subclass). In defining a class with built in
language inheritance (i.e. class B1(A)), all the private variables and
methods of the super class are exposed to the base class, so the
subclass can use details of the implementation of the super class in
its own implementation. Make sense? This is white box inheritance, i.e.
everything is exposed to the subclass. The other, Black Box
inheritance, happens when the "subclass" contains an instance of the
super class. Then the subclass will use delegation to expose the
methods of the super class (or override them, add to them, etc). This
black box style is more sound in terms of abstraction and modularity,
as white box inheritance usually leads to more implementation
dependencies between the subclass and super class (which can break the
subclass when the super class implementation changes), while black box
inheritance uses the interface of the super class to interact with it
(hence allowing you to replace the super class of the subclass with any
class that has the same interface). However, in the black box style, it
can be a pain to update the interface of the sub class every time you
add some other functionality to the super class, i.e. if you create a
new method in A called foo2(), it would automatically be accessible in
B1, but you have to create a new method also called foo2() in B2 that
delegates to A.
 
D

David Ells

Carl said:
I think it's kind of a fine point. In my own code I've had cases where
I've switched from subclass to attribute and back over the development,
and vice versa. I think there are many cases where it's preferable to
use an attribute, but not really wrong to subclass (and vice versa).

The classical advice in choosing whether to subclass or or use
attribute is whether its more an an "is a" or "has a" relationship. If
it's more natural to say B is an A, then subclass. If it's more
natural to say B has an A, then use an attribute. But that's only a
rule of thumb.

I personally find another question helpful. If it's reasonable that B
could have more than one of A, regardless if it actually does, use an
attribute. If it's unreasonable, subclass. Again, rule of thumb.


Carl Banks

This is not always the defining line between when to use inheritance
vs. when to use composition. It is of course the way to decide among
the two in the situation of 'has a' vs. 'is a', i.e. a car with an
engine instance ( a car has an engine) vs. a ford mustang engine which
is an engine. But even in the second case, you may not automatically
want to use the built in language inheritance, like class B1(A).
Instead, you may want to use a private instance of the super class and
delegate calls from the subclass to it, in order to preserve the
abstraction barrier that the interface of the super class has put up.
Then a ford mustang engine, which is still an engine, is simply a class
with a private engine instance that delegates the appropriate calls to
that instance. The rule of 'has a' and 'is a' still holds, but there is
also more than one way to do inheritance (language-facilitated vs.
composition), and while this would normally be a peripheral point, this
is basically what the O.P. was asking about (i.e. the difference
between classes B1 and B2, and how they go about subclassing A).
 
C

Carl Banks

David said:
Carl Banks wrote:

This is not always the defining line between when to use inheritance
vs. when to use composition.

I really don't think a defining line exists. Some situatations exist
where either will do, and as I've said I've often switched between them
as my code develops. Sometimes the best choice is due to some
technicality.

"is a" vs "has a" is only a guideline, in situations where it isn't
obvious, to hopefully but not certainly avoid a future switch. Same
thing for other guideline I posted. If the guideline turns out to pick
the wrong way, big deal, you fix it when refactoring.

You're not afraid of refactoring, are you? :)


Carl Banks
 
D

damacy

Amit said:
I expect that you would know the IP range for your network. Then you
can simply 'ping' each IP in the range to find wether its alive.
Moreover by your description I guess you would actually want to find
all machines in your network that run a particular network service, to
allow you to "distribute the database". In such case you can use
"nmap" with -p option, to find all the machines which are listening on
the particular port.

hth,
amit.
It seems that I am not too busy, so here is a code which may work with
a few tweaks here and there:
_________________________________________________________________________
import os
# base and range of the ip addresses
baseIP = "10.0.0."
r = 6
interestingPort = 22 # port that you want to scan
myIPs = []

for i in range(r):
ip = baseIP+str(i) # It may need some customization for your case
print "scanning: %s" %(ip)
for output in os.popen("nmap %s -p %s" %(ip,
interestingPort)).readlines():
if output.__contains__('%s/tcp open'
%interestingPort): # i guess it would be tcp
myIPs.append(ip)
__________________________________________________________________________
print myIPs


hth,
amit.
--
----
Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.

thank you for your code. i had a look at nmap and i think it's got some
cool features in it. however, i found it quite slow in my case as it
takes extra time to process the output.

in my program so far, multiple threads (255 threads in total) spawned
at once with each one of them trying to call socket.gethostbyaddr(ip)
function. i.e. if exception thrown, no machine found. i used .join() to
wait for the threads to terminate. it's fully working however the
problem is that it's too slow. it takes approx. 14 seconds to process
(i tried using 'ping' but it's even slower.).

my question is.. is there a way to improve performance of the program
if i know what the port number would be? in my case, the port number
will always be constant although i have no clue on what ip addresses
would be (that's the reason all of 255 different addresses must be
tested).

i tried the same function with the port number specified,
gethostbyaddr(ip:portno), but it is even 10-second slower than using
the same function without a port number specified (i.e. approx. 25
seconds to complete).

could anyone think of a better way of solving this problem?

regards, damacy
 
A

Amit Khemka

in my program so far, multiple threads (255 threads in total) spawned
at once with each one of them trying to call socket.gethostbyaddr(ip)
function. i.e. if exception thrown, no machine found. i used .join() to
wait for the threads to terminate. it's fully working however the
problem is that it's too slow. it takes approx. 14 seconds to process
(i tried using 'ping' but it's even slower.).

my question is.. is there a way to improve performance of the program
if i know what the port number would be? in my case, the port number
will always be constant although i have no clue on what ip addresses
would be (that's the reason all of 255 different addresses must be
tested).

i tried the same function with the port number specified,
gethostbyaddr(ip:portno), but it is even 10-second slower than using
the same function without a port number specified (i.e. approx. 25
seconds to complete).

You can save some (DNS) overheads by escaping the call
"gethostbyaddr", assuming you are not interested in knowing the
'Names' of the machines in your Network. And directly attempt to find
the machines which are listenting on the specified port. A simple way
of
doing this would be to use socket.connect((ip, port)), if the
connections succeds you have
your machine !

( There are various other ways of scanning ports, have a look at:
http://insecure.org/nmap/nmap_doc.html#connect )

Though I am not sure how 'fast' it would be. Also remember that the
time in scanning is affected by network-type,
response-time-of-remote-machine, number-of-machines scanned etc.

I would still be interested, in seeing how nmap(with some smart
options) compares with the python code. ( In my network "nmap
-osscan_limit -p 22 -T5 Class_D_Network" completes in 1.5 seconds !)

cheers,
amit.
--
 
G

Gabriel Genellina

in my program so far, multiple threads (255 threads in total) spawned
at once with each one of them trying to call socket.gethostbyaddr(ip)
function. i.e. if exception thrown, no machine found. i used .join() to
wait for the threads to terminate. it's fully working however the
problem is that it's too slow. it takes approx. 14 seconds to process
(i tried using 'ping' but it's even slower.).

Do you have control over the server program?
Use UPD to broadcast a message, and make the server answer on that.
Or google for ZeroConf, UPnP or other techniques used for service discovery.



Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
D

damacy

Amit said:
You can save some (DNS) overheads by escaping the call
"gethostbyaddr", assuming you are not interested in knowing the
'Names' of the machines in your Network. And directly attempt to find
the machines which are listenting on the specified port. A simple way
of
doing this would be to use socket.connect((ip, port)), if the
connections succeds you have
your machine !

( There are various other ways of scanning ports, have a look at:
http://insecure.org/nmap/nmap_doc.html#connect )

Though I am not sure how 'fast' it would be. Also remember that the
time in scanning is affected by network-type,
response-time-of-remote-machine, number-of-machines scanned etc.

I would still be interested, in seeing how nmap(with some smart
options) compares with the python code. ( In my network "nmap
-osscan_limit -p 22 -T5 Class_D_Network" completes in 1.5 seconds !)

cheers,
amit.
--
----
Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.

hello.

here are my test results;

1. using nmap with the options specified above: approx. 50 seconds or
longer
2. using socket.connect((ip, port)): approx. 26 seconds
3. using socket.gethostbyaddr(ip): approx. 14 seconds

all three above use multiple threads.

and also, i tried using (in and out) queues to detect the threads
termination (instead of .join()), however, it's also very slow and it
needs to have deadlock detection mechanism implemented in it which
would probably lower the performance of the program.

hmm...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top