Using TCP/IP

J

jblazi

How do I find out my own IP number (using Pythoi of course). And how do I
send/receive data to/from andother TCP/IP socket (of known ip number)?

I mean: which module do I have to use for that?

TIA,

JB
 
C

Christos TZOTZIOY Georgiou

How do I find out my own IP number (using Pythoi of course).

One way that works on most systems is:

import socket
socket.gethostbyname(socket.gethostname())

Basically, this way you request the IP address for your host name; so
you depend on many variables: your system has a name that can be
resolved locally by a file or DNS lookup or similar...
And how do I
send/receive data to/from andother TCP/IP socket (of known ip number)?
I mean: which module do I have to use for that?

The socket module. Check the socket.socket class.
 
J

jblazi

import socket
socket.gethostbyname(socket.gethostname())

Thx. But this does not seem to work correctly. When I start the program
and print the result, I get an IP-address but it is not the correct one
i.e. not the one I am currently using.

JB
 
P

Peter Hansen

jblazi said:
How do I find out my own IP number (using Pythoi of course). And how do I
send/receive data to/from andother TCP/IP socket (of known ip number)?

A computer can have more than one IP address. How do you want
to deal with the situation where there are multiple interfaces
with different addresses?

Or is this just for personal code? In which case, please specify the
OS on which you plan to run it.

-Peter
 
T

Tim Roberts

jblazi said:
Thx. But this does not seem to work correctly. When I start the program
and print the result, I get an IP-address but it is not the correct one
i.e. not the one I am currently using.

Perhaps you had better define "the one I am currently using" and how you
are determining it. That call will give you the IP address that your
computer thinks it is using. If you have gateways or firewalls, that might
be completely unrelated to your external IP address.
 
D

Dennis Lee Bieber

Tim Roberts fed this fish to the penguins on Wednesday 01 October 2003
08:19 pm:

Perhaps you had better define "the one I am currently using" and how
you
are determining it. That call will give you the IP address that your
computer thinks it is using. If you have gateways or firewalls, that
might be completely unrelated to your external IP address.

Perhaps parsing the output of ifconfig (if the user has privileges for
that), specifying the interface of interest, will supply the desired
data.

--
 
J

jblazi

A computer can have more than one IP address. How do you want
to deal with the situation where there are multiple interfaces
with different addresses?

Or is this just for personal code? In which case, please specify the
OS on which you plan to run it.

Thx!

It should run on MSW, probably XP. I am teaching a computer class at high
school this year and of course we are using Python. (In my view, Python is
the most wonderful Lisp which has ever existed and I am a bit sorry that
the Lisp features of Python seem to become deprecated!)

First I wanted to write my own course but the official
tutorial is virtually perfect. The first program we shall write is a
mastermind program and it would be nice if my pupils could play that over
the internet too. The first point is that they have not permanent ip
numbers so the ip number has to be found out first.

Janos Blazi
 
P

Peter Hansen

jblazi said:
First I wanted to write my own course but the official
tutorial is virtually perfect. The first program we shall write is a
mastermind program and it would be nice if my pupils could play that over
the internet too. The first point is that they have not permanent ip
numbers so the ip number has to be found out first.

Most programs don't try to figure out the IP address of the machine on which
they are running. Instead, they require the IP address of the *other* machine
to which they will talk. For that, you could just have the students type
"ipconfig" at a command line and save yourself the trouble. As others have
pointed out, in many situations, probably such as would always exist at a
school, machines on the network are not given "routable" (public) IP addresses
and you couldn't connect to them from the Internet anyway.

Another approach is to have a central server which handles the match-making
process. Then the only thing needed is the address of that server.

My suggestion is you cut scope and skip this whole aspect, and just
assume the address information is available externally to the program.
That's how 99.9% of similar software works.

-Peter
 
J

jblazi

Most programs don't try to figure out the IP address of the machine on which
they are running. Instead, they require the IP address of the *other* machine
to which they will talk. For that, you could just have the students type
"ipconfig" at a command line and save yourself the trouble. As others have
pointed out, in many situations, probably such as would always exist at a
school, machines on the network are not given "routable" (public) IP addresses
and you couldn't connect to them from the Internet anyway.

Another approach is to have a central server which handles the match-making
process. Then the only thing needed is the address of that server.

My suggestion is you cut scope and skip this whole aspect, and just
assume the address information is available externally to the program.
That's how 99.9% of similar software works.

Thx.
So I shall do this. When I call ipconfig, I see two ip adresses:


Ethernetadapter LAN-Verbindung:

Verbindungsspezifisches DNS-Suffix:
IP-Adresse (Autokonfig.). . . . . : xxx.xxx.xxx.xxx
Subnetzmaske. . . . . . . . . . . : xxx.xxx.0.0
Standardgateway . . . . . . . . . :

PPP-Adapter XXXXXX:

Verbindungsspezifisches DNS-Suffix:
IP-Adresse. . . . . . . . . . . . : xxx.xxx.xxx.xxx
Subnetzmaske. . . . . . . . . . . : 255.255.255.255
Standardgateway . . . . . . . . . : xxx.xxx.xxx.xxx

Python returns the first ip number but I should need the second one. (I
replaced the digits by 'x'.

JB
 
D

Dennis Lee Bieber

jblazi fed this fish to the penguins on Thursday 02 October 2003 07:10
am:

First I wanted to write my own course but the official
tutorial is virtually perfect. The first program we shall write is a
mastermind program and it would be nice if my pupils could play that
over the internet too. The first point is that they have not permanent
ip numbers so the ip number has to be found out first.
Well, discovering the IP of one's own net connection is still not
going to be that useful from inside the code -- you still have to have
some means to give that IP to whoever will be on the other end of the
connection, and you won't have their IP unless they've first given it
to you.

Are these dial-ups, or a classroom LAN? If the latter, you might have
one machine with a fixed IP (probably the DHCP server machine), and can
install a simple server that merely echoes back the IP of the
connecting client.

If the students are having to sneaker-net their IPs between each
other, and if they have access/privileges to run ipconfig, that should
give the information needed.

--
 
I

Irmen de Jong

jblazi said:
tutorial is virtually perfect. The first program we shall write is a
mastermind program and it would be nice if my pupils could play that over
the internet too.

Are you sure you want to do this in the 'first program you write'?
Coding a master mind program is hard as it is (for a first program),
let alone making it work over the (inter)net.

If you want to teach network programming stuff, well,
you may ignore the rest of this message ;-)

But if you just want it to work over the network, without
actually concerning yourself with the -gory- details of
network programming, please consider using Pyro for the
remote communication stuff in your program.
http://pyro.sourceforge.net

With a few extra lines in your python source you can
talk to another Python program somewhere in the network,
without writing *any* network specific communication stuff.

--Irmen de Jong
 
J

jblazi

Well, discovering the IP of one's own net connection is still not
going to be that useful from inside the code -- you still have to have
some means to give that IP to whoever will be on the other end of the
connection, and you won't have their IP unless they've first given it
to you.

Are these dial-ups, or a classroom LAN? If the latter, you might have
one machine with a fixed IP (probably the DHCP server machine), and can
install a simple server that merely echoes back the IP of the
connecting client.

If the students are having to sneaker-net their IPs between each
other, and if they have access/privileges to run ipconfig, that should
give the information needed.

Yes, thx. The pupils will be sitting at home and will dial up each other
to communicate their own ip number. But it would be nice if the started
the mastermind program and the program would say: "Hullo, your ip number
is ..., please communicate this to your mate." Of course they can use
ipconfig for finding out their data. But it would be more elegant if they
were told by their program.

JB
 
J

jblazi

Are you sure you want to do this in the 'first program you write'?
Coding a master mind program is hard as it is (for a first program),
let alone making it work over the (inter)net.

First the program will work in the class room. And then, some time later,
it should work over the internet. That would impress the pupils.

JB
 
D

Donn Cave

jblazi said:
So I shall do this. When I call ipconfig, I see two ip adresses:


Ethernetadapter LAN-Verbindung:

Verbindungsspezifisches DNS-Suffix:
IP-Adresse (Autokonfig.). . . . . : xxx.xxx.xxx.xxx
Subnetzmaske. . . . . . . . . . . : xxx.xxx.0.0
Standardgateway . . . . . . . . . :

PPP-Adapter XXXXXX:

Verbindungsspezifisches DNS-Suffix:
IP-Adresse. . . . . . . . . . . . : xxx.xxx.xxx.xxx
Subnetzmaske. . . . . . . . . . . : 255.255.255.255
Standardgateway . . . . . . . . . : xxx.xxx.xxx.xxx

Python returns the first ip number but I should need the second one. (I
replaced the digits by 'x'.

I imagine that more generally you will need the address associated
with the network you're using. One fairly simple way to arrive
at this is to connect to some well known service on that network,
and then get the bound connection's IP with getsockname().

Donn Cave, (e-mail address removed)
 
P

Paul Clinch

jblazi said:
Thx!

It should run on MSW, probably XP. I am teaching a computer class at high
school this year and of course we are using Python. (In my view, Python is
the most wonderful Lisp which has ever existed and I am a bit sorry that
the Lisp features of Python seem to become deprecated!)

First I wanted to write my own course but the official
tutorial is virtually perfect. The first program we shall write is a
mastermind program and it would be nice if my pupils could play that over
the internet too. The first point is that they have not permanent ip
numbers so the ip number has to be found out first.

Janos Blazi


You can use other socket module calls to get further info. eg.
('homebrew', [], ['198.192.192.2', '192.168.1.2', '192.168.1.4'])

giving you a tuple includeing a list of interface addresses on your machine.

Paul Clinch
 
D

Dennis Lee Bieber

jblazi fed this fish to the penguins on Thursday 02 October 2003 12:49
pm:
Yes, thx. The pupils will be sitting at home and will dial up each
other to communicate their own ip number. But it would be nice if the
started the mastermind program and the program would say: "Hullo, your
ip number is ..., please communicate this to your mate." Of course
they can use ipconfig for finding out their data. But it would be more
elegant if they were told by their program.
Well, it may not be the easiest thing to do, but you could probably
use a popen() call to retrieve the output of ipconfig, and then try to
parse that out (though not everyone may be using PPP, making it a tad
harder to find the right entry using code alone).

If you have one machine with a known IP/name somewhere (maybe the
school has one that is never shutdown) you could write a small echo
server that basically extracts the IP # from connection made to it,
formats it as text, and sends it back to the originator, and then
closes that connection to wait for the next.


--
 
D

Dennis Lee Bieber

Paul Clinch fed this fish to the penguins on Thursday 02 October 2003
01:52 pm:

You can use other socket module calls to get further info. eg.
('homebrew', [], ['198.192.192.2', '192.168.1.2', '192.168.1.4'])

giving you a tuple includeing a list of interface addresses on your
machine.
Interesting. Under Mandrake 8.2, the above returns an error:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
socket.herror: (4, 'No address associated with name')

--
 
W

William Trenker

Interesting. Under Mandrake 8.2, the above returns an error:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
socket.herror: (4, 'No address associated with name')

I get the same error here. But this works:
('noodle.noodle.org', ['noodle'], ['127.0.0.1'])

Linux 2.4.19; libc.so.6 2.2.5; gcc 2.95.3
GNU ld version 2.13.90.0.4 20020814
GNU Make 3.80
pkg-config 0.14.0
Python 2.3 (#1, Jul 30 2003, 12:02:14) [GCC 2.95.3 20010315 (release)]

Bill
 
D

Dennis Lee Bieber

William Trenker fed this fish to the penguins on Tuesday 07 October
2003 06:33 am:

I get the same error here. But this works:
('noodle.noodle.org', ['noodle'], ['127.0.0.1'])
Unfortunately, the original poster was looking for a way to get the IP
number of their (dynamic?) network connection. The above doesn't do
that.
import socket
socket.gethostbyname_ex(socket.gethostname()) ('beastie.elusive-unicorn.iiss', ['beastie'], ['192.168.0.1'])
socket.gethostbyname(socket.gethostname()) '192.168.0.1'
socket.gethostbyname('')
'0.0.0.0'

192.168.0.1 is my 10/100 interface, but 99.9% of the time, that is not
active. My dial-up IP can not be readily obtained.

--
 

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

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,902
Members
47,443
Latest member
RobertHaddy

Latest Threads

Top