By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

all ip addresses of machines in the local network

P: n/a
hi, there. i have a problem writing a program which can obtain ip
addresses of machines running in the same local network.

say, there are 4 machines present in the network; [a], [b], [c] and [d]
and if i run my program on [a], it should be able to find "host names"
and "ip addresses" of the other machines; [b], [c] and [d]?

i have read some threads posted on this group, however, they only work
for localhost, not the entire network.

any hints if possible?

thanks for your time.

regards, damacy

Aug 24 '06 #1
Share this Question
Share on Google+
31 Replies


P: n/a
damacy wrote:
hi, there. i have a problem writing a program which can obtain ip
addresses of machines running in the same local network.

say, there are 4 machines present in the network; [a], [b], [c] and [d]
and if i run my program on [a], it should be able to find "host names"
and "ip addresses" of the other machines; [b], [c] and [d]?

i have read some threads posted on this group, however, they only work
for localhost, not the entire network.

any hints if possible?

thanks for your time.

regards, damacy
What is this for? Some kind of high availablity server setup? I don't
know anything that would be useful to you, but I am curious, and maybe
it will clarify your intentions for others.

-Sandra

Aug 24 '06 #2

P: n/a
hi, sandra.

no, it's not as complicated as that. all i want to do is to load a
database onto different machines residing in the same network. i hope
there is a way doing it. or perhaps i have a poor understanding of how
networks work.

regards, damacy

Sandra-24 wrote:
damacy wrote:
hi, there. i have a problem writing a program which can obtain ip
addresses of machines running in the same local network.

say, there are 4 machines present in the network; [a], [b], [c] and [d]
and if i run my program on [a], it should be able to find "host names"
and "ip addresses" of the other machines; [b], [c] and [d]?

i have read some threads posted on this group, however, they only work
for localhost, not the entire network.

any hints if possible?

thanks for your time.

regards, damacy

What is this for? Some kind of high availablity server setup? I don't
know anything that would be useful to you, but I am curious, and maybe
it will clarify your intentions for others.

-Sandra
Aug 24 '06 #3

P: n/a
"damacy" <we****@gmail.comwrote:
hi, there. i have a problem writing a program which can obtain ip
addresses of machines running in the same local network.

say, there are 4 machines present in the network; [a], [b], [c] and [d]
and if i run my program on [a], it should be able to find "host names"
and "ip addresses" of the other machines; [b], [c] and [d]?

i have read some threads posted on this group, however, they only work
for localhost, not the entire network.

any hints if possible?
google for nmap, don't reinvent the wheel.
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Aug 24 '06 #4

P: n/a
On Wed, 23 Aug 2006 21:46:21 -0700, damacy wrote:
hi, sandra.

no, it's not as complicated as that. all i want to do is to load a
database onto different machines residing in the same network. i hope
there is a way doing it. or perhaps i have a poor understanding of how
networks work.
It would not be 'as complicated as that' if we knew the kind of
network you are on -- NFS, Samba, Windows, some hybred network,
SSH, FTP, telnet, remote X, remote desktops? Every service has its
own way of doing things.
Aug 24 '06 #5

P: n/a
"damacy" <we****@gmail.comwrote:
hi, sandra.
If you reply like is common on Usenet, there is no need to address
someone, since the attribution line is just there:

Sandra-24 wrote:
^^^^^^^^^^^^^^^

Google for top posting, and read why it's generally considered bad.
no, it's not as complicated as that. all i want to do is to load a
database onto different machines residing in the same network.
if they are all in the xx.yy.zz.ii range, with ii the only number that
varies, you could ping each ii with a short time out. No reply = nothing
on it (unless pings are blocked).

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Aug 24 '06 #6

P: n/a
On 23 Aug 2006 21:46:21 -0700, damacy <we****@gmail.comwrote:
hi, sandra.

no, it's not as complicated as that. all i want to do is to load a
database onto different machines residing in the same network. i hope
there is a way doing it. or perhaps i have a poor understanding of how
networks work.
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.
----
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.
Aug 24 '06 #7

P: n/a
On 8/24/06, Amit Khemka <kh********@gmail.comwrote:
On 23 Aug 2006 21:46:21 -0700, damacy <we****@gmail.comwrote:
hi, sandra.

no, it's not as complicated as that. all i want to do is to load a
database onto different machines residing in the same network. i hope
there is a way doing it. or perhaps i have a poor understanding of how
networks work.

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.
Aug 24 '06 #8

P: n/a
I am a member of another list that has at least one member
who has lost his vision, and reads his news with a speech
generator. It is terribly inconvenient for him to follow
a thread full of 'bottom postings', as he is then forced to
sit through the previous message contents again and again
in search of the new content.
Google for top posting, and read why it's generally considered bad.
--
Posted via a free Usenet account from http://www.teranews.com

Aug 24 '06 #9

P: n/a
tobiah <st@tobiah.orgwrote:
I am a member of another list that has at least one member
who has lost his vision, and reads his news with a speech
generator. It is terribly inconvenient for him to follow
a thread full of 'bottom postings', as he is then forced to
sit through the previous message contents again and again
in search of the new content.
Yes, that's why it's also recommended to *remove all lines you're not
replying to*. A full quote is always annoying (unless it's really needed
to get some context, which is in general rare).

Not only people with a vision problem will be thankful for that. Like I
recommended to *read* on top posting, instead of repeating myths that are
made up by people who are ignorant.

Simple recipe:
http://johnbokma.com/mexit/2006/04/11/how-to-reply.html

I am sure that people with a vision problem who jump into the thread
prefer a small context over listening "upside down" to get some context, I
mean:

A: top posting
Q: what is the most annoying thing on Usenet

Now imagine that Q is quote after quote after qoute because a hand ful of
lazy clueless bastards want to save time.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Aug 24 '06 #10

P: n/a
I am a member of another list that has at least one member
who has lost his vision, and reads his news with a speech
generator. It is terribly inconvenient for him to follow
a thread full of 'bottom postings', as he is then forced to
sit through the previous message contents again and again
in search of the new content.
I'm involved on the Blind Linux users (Blinux) list, and they
seem to have no problem bottom-posting.

There are a variety of tools for converting bottom-posting into
more readable formats. If you want to suppress comments, a
quality MUA can suppress them. Or you can pipe them through
sed/grep/whatever and strip out all lines beginning with a
greater-than sign. Or, use the search functionality in the
screen-reader to skip ahead to the next line until you get to one
that doesn't begin with a greater-than sign. Some text-display
areas even allow you to use searching to move the cursor. E.g.
if reading a mail in mutt in a console, you can open it in vim
and search for "^[^>]" which will move the cursor to the next
line that doesn't begin with a greater-than sign. Quite usable
from within yasr ("yet another screen reader").

Inspired by a problem discussed on the Blinux list, I've also
created a python tool for converting standard quoting notation
(using greater-than signs) into a more TTS-friendly
(text-to-speech) format:

http://www.redhat.com/archives/blinu.../msg00012.html
http://www.redhat.com/archives/blinu.../msg00015.html
http://www.redhat.com/archives/blinu.../msg00016.html

Thus, there are tools for blind/visually-impared folks that can
make it easier for them to correspond using the standards the
rest of the world uses. Even among blind users, there's often a
division between those that use Braille terminals and those that
use TTS. Those using Braille tend to fall in with the rest of
the internet, preferring bottom-posting. Those using TTS aren't
quite so fond of having their own content regurgitated back to
them. However, judicial pruning of quoted contend can ease that
problem.

Thus, there are plenty of tools to help BVI folks operate under
the standard of bottom-posting.

-tkc

Aug 24 '06 #11

P: n/a
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

Aug 24 '06 #12

P: n/a
please don't hit reply to arbitrary messages when you're posting new
messages; it messes up the message threading.

Chaz Ginger wrote:
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.
your terminology is confused: if you inherit from another class, *your*
class is the subclass, while the class you inherit from is known as a
"base class" or "super class".

a subclass will share the instance namespace with the base class, which
means, among other things, that you may accidentally override internal
attributes and methods, and thus break the base class.

and even if it appears to work now, it may break when you upgrade the
base class. or when you end up in a code path that you haven't tested
before.

</F>

Aug 24 '06 #13

P: n/a
Fredrik Lundh wrote:
please don't hit reply to arbitrary messages when you're posting new
messages; it messes up the message threading.

Chaz Ginger wrote:
>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.

your terminology is confused: if you inherit from another class, *your*
class is the subclass, while the class you inherit from is known as a
"base class" or "super class".

a subclass will share the instance namespace with the base class, which
means, among other things, that you may accidentally override internal
attributes and methods, and thus break the base class.

and even if it appears to work now, it may break when you upgrade the
base class. or when you end up in a code path that you haven't tested
before.

</F>
Sorry for the threading screw up. I thought I had just hit the write button.

I understand when my class overrides the super class. But that would
just be "normal" class related things. I was wondering if there was
something more subtle I am missing in Python class handling.

Chaz
Aug 24 '06 #14

P: n/a
Fredrik Lundh wrote:
please don't hit reply to arbitrary messages when you're posting new
messages; it messes up the message threading.

Chaz Ginger wrote:
>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.

your terminology is confused: if you inherit from another class, *your*
class is the subclass, while the class you inherit from is known as a
"base class" or "super class".

a subclass will share the instance namespace with the base class, which
means, among other things, that you may accidentally override internal
attributes and methods, and thus break the base class.

and even if it appears to work now, it may break when you upgrade the
base class. or when you end up in a code path that you haven't tested
before.

</F>
Sorry for the threading screw up. I thought I had just hit the write button.

I understand when my class overrides the super class. But that would
just be "normal" class related things. I was wondering if there was
something more subtle I am missing in Python class handling.

Chaz
Aug 24 '06 #15

P: n/a
At Thursday 24/8/2006 16:23, Chaz Ginger wrote:
>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.

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

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?
Look any OO book for the difference between 'inheritance' and
'delegation'. In short, you should inherit when B 'is an' A (a Car is
a Vehicle), and delegate/compose in other cases (a Car has an Engine;
or more precisely, a Car instance has an Engine instance).
Gabriel Genellina
Softlab SRL

p5.vert.ukl.yahoo.com uncompressed Thu Aug 24 19: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

Aug 24 '06 #16

P: n/a

Chaz Ginger wrote:
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.
[snip]
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?
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

Aug 24 '06 #17

P: n/a
Gabriel Genellina wrote:
At Thursday 24/8/2006 16:23, Chaz Ginger wrote:
>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.

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

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?
Look any OO book for the difference between 'inheritance' and
'delegation'. In short, you should inherit when B 'is an' A (a Car is a
Vehicle), and delegate/compose in other cases (a Car has an Engine; or
more precisely, a Car instance has an Engine instance).
Gabriel Genellina
Softlab SRL


p5.vert.ukl.yahoo.com uncompressed Thu Aug 24 19: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
That is merely a logical use of OO after all when would a car and an
orange be the same?

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).

Chaz

Aug 24 '06 #18

P: n/a
Gabriel Genellina wrote:
At Thursday 24/8/2006 16:23, Chaz Ginger wrote:
>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.

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

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?
Look any OO book for the difference between 'inheritance' and
'delegation'. In short, you should inherit when B 'is an' A (a Car is a
Vehicle), and delegate/compose in other cases (a Car has an Engine; or
more precisely, a Car instance has an Engine instance).
Gabriel Genellina
Softlab SRL


p5.vert.ukl.yahoo.com uncompressed Thu Aug 24 19: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
That is merely a logical use of OO after all when would a car and an
orange be the same?

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).

Chaz
Aug 24 '06 #19

P: n/a
Tim Chase <py*********@tim.thechases.comwrites:
I am a member of another list that has at least one member who has
lost his vision, and reads his news with a speech generator. It
is terribly inconvenient for him to follow a thread full of
'bottom postings', as he is then forced to sit through the
previous message contents again and again in search of the new
content.

I'm involved on the Blind Linux users (Blinux) list, and they seem
to have no problem bottom-posting.

There are a variety of tools for converting bottom-posting into more
readable formats. If you want to suppress comments, a quality MUA
can suppress them.
The parent poster is complaining about a straw man. As they point out,
"bottom posting" is almost as bad as top posting. Both practices leave
the entire content of quoted material intact, regardless of which
parts are relevant.

The correct solution to both top posting *and* bottom posting is
"interleaved posting", but more importantly to trim away everything
except the quoted material necessary for giving context to one's
response. This is of even greater assistance to the blind, who then
have just the necessary context to understand the current message.

<URL:http://en.wikipedia.org/wiki/Top_posting>

--
\ "We are not gonna be great; we are not gonna be amazing; we are |
`\ gonna be *amazingly* amazing!" -- Zaphod Beeblebrox, _The |
_o__) Hitch-Hiker's Guide To The Galaxy_, Douglas Adams |
Ben Finney

Aug 24 '06 #20

P: n/a
Chaz Ginger wrote:
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

Aug 24 '06 #21

P: n/a
At Thursday 24/8/2006 17:44, Chaz Ginger wrote:
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.

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

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?
Look any OO book for the difference between 'inheritance' and
'delegation'. In short, you should inherit when B 'is an' A (a Car is a
Vehicle), and delegate/compose in other cases (a Car has an Engine; or
more precisely, a Car instance has an Engine instance).

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

Aug 24 '06 #22

P: n/a
Gabriel Genellina <ga******@yahoo.com.arwrote in
news:ma***************************************@pyt hon.org:
At Thursday 24/8/2006 17:44, Chaz Ginger wrote:
>>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've heard of cars that seem to inherit from the *lemon* class, though. Not
a good object model, that.

--
rzed
Aug 24 '06 #23

P: n/a
At Thursday 24/8/2006 19:51, Chaz Ginger wrote:
>>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.

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

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?

Look any OO book for the difference between 'inheritance' and
'delegation'. In short, you should inherit when B 'is an' A (a Car is a
Vehicle), and delegate/compose in other cases (a Car has an Engine; or
more precisely, a Car instance has an Engine instance).

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.

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

Aug 25 '06 #24

P: n/a
Amit Khemka wrote:
On 23 Aug 2006 21:46:21 -0700, damacy <we****@gmail.comwrote:
>hi, sandra.

no, it's not as complicated as that. all i want to do is to load a
database onto different machines residing in the same network. i hope
there is a way doing it. or perhaps i have a poor understanding of how
networks work.


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...ution_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.

Aug 25 '06 #25

P: n/a
Chaz Ginger wrote:
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.

Aug 25 '06 #26

P: n/a
Carl Banks wrote:
>
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).

Aug 25 '06 #27

P: n/a

David Ells wrote:
Carl Banks wrote:
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.

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

Aug 25 '06 #28

P: n/a
Amit Khemka wrote:
On 8/24/06, Amit Khemka <kh********@gmail.comwrote:
On 23 Aug 2006 21:46:21 -0700, damacy <we****@gmail.comwrote:
hi, sandra.
>
no, it's not as complicated as that. all i want to do is to load a
database onto different machines residing in the same network. i hope
there is a way doing it. or perhaps i have a poor understanding of how
networks work.
>
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

Aug 31 '06 #29

P: n/a
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.
--
----
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.
Aug 31 '06 #30

P: n/a
At Wednesday 30/8/2006 21:54, damacy wrote:
>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

Aug 31 '06 #31

P: n/a
Amit Khemka wrote:
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.
--
----
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...

Sep 1 '06 #32

This discussion thread is closed

Replies have been disabled for this discussion.