Ip address

S

Scripter47

How do i get my ip address?

in cmd.exe i just type "ipconfig" then it prints:
...
IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
...
how can i do that in python??
 
K

Klaus Alexander Seistrup

Scripter47 said:
How do i get my ip address?

in cmd.exe i just type "ipconfig" then it prints:
...
IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
...
how can i do that in python??

#v+

python -c 'import re, urllib; print re.findall("<title>Your IP: (.+?)</title>", urllib.urlopen("http://myip.dk/").read())[0]'

#v-

Cheers,
 
S

Scripter47

Klaus Alexander Seistrup skrev:
Scripter47 said:
How do i get my ip address?

in cmd.exe i just type "ipconfig" then it prints:
...
IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
...
how can i do that in python??

#v+

python -c 'import re, urllib; print re.findall("<title>Your IP: (.+?)</title>", urllib.urlopen("http://myip.dk/").read())[0]'

#v-

Cheers,
Hmm then you need Internet connecting. can i do it without that? ´

and it doesn't work either :(
 
K

Klaus Alexander Seistrup

Scripter47 said:
python -c 'import re, urllib; print re.findall("<title>Your IP: (.+?)</title>", urllib.urlopen("http://myip.dk/").read())[0]'

Hmm then you need Internet connecting.

That's what IP adresses are for...
can i do it without that?

Perhaps you could use the method mentioned in
http://mail.python.org/pipermail/python-list/1999-August/009153.html
and it doesn't work either :(

Works for me:

#v+

klaus@home:~ $ python -c 'import re, urllib; print re.findall("<title>Your IP: (.+?)</title>", urllib.urlopen("http://myip.dk/").read())[0]'
217.157.1.202
klaus@home:~ $

#v-

Cheers,
 
A

Adam

Hey,

This will get your IP address:

#######Code########
print socket.gethostbyaddr(socket.gethostname())
('compname', [], ['192.168.1.2'])
########End Code########

If you are wanting to to communicate over the internet you will have
to get the IP of you rounter. So you will have to either find a way to
talk to your router or try and use an online service like these other
guys suggest.
 
C

Colin J. Williams

Klaus said:
Scripter47 said:
How do i get my ip address?

in cmd.exe i just type "ipconfig" then it prints:
...
IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
...
how can i do that in python??

#v+

python -c 'import re, urllib; print re.findall("<title>Your IP: (.+?)</title>", urllib.urlopen("http://myip.dk/").read())[0]'

#v-

Cheers,
Klaus,

Your one-liner doesn't work for me, with Windows XP, but the following
does, within Python.

*** Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32. ***
Colin W.
 
K

Klaus Alexander Seistrup

Adam said:
This will get your IP address:

#######Code########
print socket.gethostbyaddr(socket.gethostname())
('compname', [], ['192.168.1.2'])
########End Code########

It will return an IP address, but not necessarily the one you want:

#v+

klaus@home:~ $ python -c 'import socket; print socket.gethostbyaddr(socket.gethostname())'
('zdani.szn.dk', [], ['2001:1448:89::1'])
klaus@home:~ $

#v-

Cheers,
 
K

Klaus Alexander Seistrup

Colin said:
Your one-liner doesn't work for me, with Windows XP, but the
following does, within Python.

Could it be due to shell-escaping issues? I don't know anything
about Windows...

Cheers,
 
S

Steve Holden

Adam said:
Hey,

This will get your IP address:

#######Code########
print socket.gethostbyaddr(socket.gethostname())
('compname', [], ['192.168.1.2'])
########End Code########

If you are wanting to to communicate over the internet you will have
to get the IP of you rounter. So you will have to either find a way to
talk to your router or try and use an online service like these other
guys suggest.

There is absolutely no need to know the IP address of "your router" to
communicate with Internet devices. Either your IP layer is configured to
know the addresses of one or more routers, or it has discovered those
address by dynamic means, or you can't get off-net because there aren't
any routers.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007
 
T

Toby A Inkster

Steve said:
There is absolutely no need to know the IP address of "your router" to
communicate with Internet devices. Either your IP layer is configured to
know the addresses of one or more routers, or it has discovered those
address by dynamic means, or you can't get off-net because there aren't
any routers.

.... or you can't get off-net because you don't *know* the routers.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
 
T

Toby A Inkster

Scripter47 said:
How do i get my ip address?

Which IP address. One computer might have many IP addresses. (Indeed a
typical network-connected computer will tend to have at least one for each
connected network device, plus the special address 127.0.0.1 for the
loopback network.) How is Python supposed to know which IP address you
want?

If you don't care which address, the code supplied by Adam should work. If
you do care, then you'll probably need to write OS-specific code for each
platform you choose to support, probably parsing the output of ipconfig
(Windows) or ifconfig (Linux/UNIX/Mac) somehow.

Adam also says:
| If you are wanting to to communicate over the internet you will have
| to get the IP of you rounter.

Not strictly true, but if your network uses NAT, and you want some host
outside your network to be able to *connect to you*, then yes, you need the
IP address of your router. If you're not using NAT, then you shouldn't
need to worry about your router, as IP addresses alone provide full
end-to-end routing. (Indeed that's the whole point of IP.)

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
 
G

Gabriel Genellina

At said:
Your one-liner doesn't work for me, with Windows XP, but the following

On XP you should switch the usage of " and ' (double quotes are used
to enclose command arguments with embedded spaces):

python -c "import re, urllib; print re.findall('<title>Your IP:
(.+?)</title>', urllib.urlopen('http://myip.dk/').read())[0]"


--
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
 
B

Beej

Scripter47 said:
How do i get my ip address?
in cmd.exe i just type "ipconfig" then it prints:
...
IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
...
how can i do that in python??#v+

python -c 'import re, urllib; print re.findall("<title>Your IP: (.+?)</title>", urllib.urlopen("http://myip.dk/").read())[0]'

This is extremely unlikely to return 192.168.1.10. :)

(It will give you the address of your firewall or whatever is your
gateway to the outside world... which is a cool thing to know, but
I'm not sure it's what the op's after.)

-Beej
 
A

Adonis Vargas

Scripter47 said:
How do i get my ip address?

in cmd.exe i just type "ipconfig" then it prints:
...
IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
...
how can i do that in python??

If you want to get your external IP you can do:

import urllib

checkIP = urllib.urlopen("http://checkip.dyndns.org").read()
externalIP = checkIP.split()[-1].strip("</body></html>")
print externalIP

Hope this helps.

Adonis
 
S

Scripter47

Beej skrev:
Scripter47 said:
How do i get my ip address?
in cmd.exe i just type "ipconfig" then it prints:
...
IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
...
how can i do that in python??#v+
python -c 'import re, urllib; print re.findall("<title>Your IP: (.+?)</title>", urllib.urlopen("http://myip.dk/").read())[0]'

This is extremely unlikely to return 192.168.1.10. :)

(It will give you the address of your firewall or whatever is your
gateway to the outside world... which is a cool thing to know, but
I'm not sure it's what the op's after.)

-Beej

Exactly i wnat my local ip. sorry if i din't tell that ;)
 
S

Steve Holden

Toby said:
... or you can't get off-net because you don't *know* the routers.
What I know or don't know makes absolutely no difference to whether my
computer can reach the Internet, it's a matter of whether the IP layes
is configured to know the appropriate address to which it can hand off
non-local traffic.

If you are trying to say that it's necessary to know the IP address of
the routers in order to select a specific interface address from the
available choices as "the Internet interface" then kindly say so and
stop wallowing in semantic obscurity.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007
 

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
474,239
Messages
2,571,200
Members
47,836
Latest member
Stuart66

Latest Threads

Top