finding IP address of computer

C

Chris

How do I find and print to screen the IP address of the computer my
python program is working on?
 
G

Gregor Horvath

Chris said:
How do I find and print to screen the IP address of the computer my
python program is working on?

IP adresses are bound to network interfaces not to computers.
One Computer can have multiple network interfaces.
 
J

Jorge Godoy

Chris said:
hehe, works a charm, cheers mate.

Beware that if you have a different entry in your hosts file you can match a
different name.

Test it:

- add "127.0.0.2 yourhost.yourdomain yourhost" to /etc/hosts
- rerun the code.

You'll see "127.0.0.2" as the result. So take that into account.

--
Jorge Godoy <[email protected]>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
 
G

Grant Edwards

IP adresses are bound to network interfaces not to computers.
One Computer can have multiple network interfaces.

And each interface can have any number if IP addresses
(including none).
 
T

Terry Reedy

Grant Edwards said:
And each interface can have any number if IP addresses
(including none).

To answer the OP for typical situations: if you are accessing the internet
via a local network, the network administrator should be able to tell you.
In fact, for some networks, the IP address is part of the interface setup.
If the network is run by a router, it should be able to tell you. If you
are sitting behind a NAT (network address translation) router and you want
to know the external address, there are web pages that echo your externally
visible address back to you.

Terry Jan Reedy
 
D

DarkBlue

Chris said:
How do I find and print to screen the IP address of the computer my
python program is working on?

def readip():
import re, urllib
f = urllib.urlopen('http://checkip.dyndns.org')
s = f.read()
m = re.search('([\d]*\.[\d]*\.[\d]*\.[\d]*)', s)
return m.group(0)

myip = readip()
 

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,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top