473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Ip address

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

Jan 28 '07 #1
16 2408
Scripter47 wrote:
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 Alexander Seistrup
http://klaus.seistrup.dk/

Jan 28 '07 #2
Klaus Alexander Seistrup skrev:
Scripter47 wrote:
>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 :(

Jan 28 '07 #3
Scripter47 wrote:
>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/pyt...st/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,

--
Klaus Alexander Seistrup
http://klaus.seistrup.dk/

Jan 28 '07 #4
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.

On Jan 28, 9:54 am, Scripter47 <nos...@riddergarn.dkwrote:
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??
Jan 28 '07 #5
Klaus Alexander Seistrup wrote:
Scripter47 wrote:
>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. ***
>>import re, urllib; print re.findall("<title>Your IP:
(.+?)</title>", urllib.urlopen("http://myip.dk/").read())[0]
69.157.68.189
>>>
Colin W.

Jan 28 '07 #6
Adam wrote:
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,

--
Klaus Alexander Seistrup
http://klaus.seistrup.dk/

Jan 28 '07 #7
Colin J. Williams wrote:
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,

--
Klaus Alexander Seistrup
http://klaus.seistrup.dk/

Jan 28 '07 #8
Adam wrote:
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

Jan 28 '07 #9
Steve Holden wrote:
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!
Jan 28 '07 #10
Scripter47 wrote:
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!
Jan 28 '07 #11
At Sunday 28/1/2007 10:28, Colin J. Williams wrote:
>Klaus Alexander Seistrup wrote:

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

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

Jan 29 '07 #12
Klaus Alexander Seistrup wrote:
urllib.urlopen("http://myip.dk/")
http://whatismyip.org gives it to you in a more usable format. But, as
others have pointed out, it might return your router's IP.

--
Garry Knight
ga*********@gmx.net
Jan 29 '07 #13
On Jan 28, 2:26 am, Klaus Alexander Seistrup <k...@seistrup.dkwrote:
Scripter47 wrote:
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

Jan 29 '07 #14
Scripter47 wrote:
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
Jan 29 '07 #15
Beej skrev:
On Jan 28, 2:26 am, Klaus Alexander Seistrup <k...@seistrup.dkwrote:
>Scripter47 wrote:
>>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 ;)

Jan 29 '07 #16
Toby A Inkster wrote:
Steve Holden wrote:
>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.
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

Jan 30 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port...
8
by: YAN | last post by:
Hi, I want to get the mac address from a machine, which i have the IP address of that machine, how can i do that? I know how to get the mac address of the local machine from the following code: ...
7
by: Privacy Advocate | last post by:
//crossposted to: comp.lang.javascript, alt.comp.lang.javascript in an effort to get factual answers from JavaScript experts// Simply put; Is it possible to obtain the real (actual) IP address of...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
4
by: andreas.w.h.k. :-\) | last post by:
How do I change the address location in the wsdl <wsdl:port name="SearchSoap12" binding="tns:SearchSoap12"> <soap12:address location="http://searchservices/engine/search.asmx" /> </wsdl:port> ...
1
by: Phoenix_ver10 | last post by:
I have a mailing list with multiple names going to the same addresses. I need one address with all the names for that address on it. I checked out the example on microsoft's site, but A: It doesn't...
1
by: Jamie J. Begin | last post by:
I'm very new to the world of Python and am trying to wrap my head around it's OOP model. Much of my OOP experience comes from VB.Net, which is very different. Let's say I wanted to create an...
6
by: Nicolas Noakes | last post by:
Hello, I would like to convert to following process to code. Any advice is welcome. I have a hardware device which requires the this procedure to set it's IP address. First create an static...
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
1
by: saravanatmm | last post by:
I need javascript code for validate the email address. Email address field cannot allowed the capital letters, special characters except '@' symbol. But can allowed the small letters, numeric...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.