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

Best way to get ip address

P: n/a
Hello list:

I am writing a small app in python which tracks the kernel banner at
kernel.org and downloads newer kernel version(s) (if there are any).

I am using GeoIP to set the download to a local mirror if there is one. This
is what I use to get the local IP address:

ip = socket.gethostbyaddr(socket.gethostname())
# returns: ('hostname.domain', ['hostname'], ['192.168.0.2'])
ip = str(ip[2])
# returns: "['192.168.0.2']"
ip = ip[2:-2]
# returns: '192.168.0.2'

This works fine, but seems a little crufty and convoluted, and usually when
things get crufty it means there is a better way to do it...so is there a
preferred way of obtaining a local IP address?

Another issue is that if the user is behind a NAT firewall or somesuch and
uses a private network address then the whole GeoIP code is moot. I was
thinking that if this is the case then the app could check for the default
gateway of the machine and use that IP instead, but of course the gateway may
just be another private network IP address. Does anyone have some ideas on
how I could make this code useful with a private IP address?

Thanks for your time.
--
Part of the problem since 1976
http://badcomputer.no-ip.com
Get my public key from
http://keyserver.linux.it/pks/lookup...earch=bulliver
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBBQKoLOrzWcOwL7mwRAq3sAKCT83z/W3nlZF321xlsAonWhorg3gCfTGmy
iyWScjtbCNW1h3ssjnFm2q0=
=Hux5
-----END PGP SIGNATURE-----

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <ma**************************************@python.o rg>,
Darren Kirby <bu******@badcomputer.no-ip.com> wrote:
-=-=-=-=-=-

Hello list:

I am writing a small app in python which tracks the kernel banner at
kernel.org and downloads newer kernel version(s) (if there are any).

I am using GeoIP to set the download to a local mirror if there is one. This
is what I use to get the local IP address:

ip = socket.gethostbyaddr(socket.gethostname())
# returns: ('hostname.domain', ['hostname'], ['192.168.0.2'])
ip = str(ip[2])
# returns: "['192.168.0.2']"
ip = ip[2:-2]
# returns: '192.168.0.2'

This works fine, but seems a little crufty and convoluted, and usually when
things get crufty it means there is a better way to do it...so is there a
preferred way of obtaining a local IP address?

Another issue is that if the user is behind a NAT firewall or somesuch and
uses a private network address then the whole GeoIP code is moot. I was
thinking that if this is the case then the app could check for the default
gateway of the machine and use that IP instead, but of course the gateway may
just be another private network IP address. Does anyone have some ideas on
how I could make this code useful with a private IP address?

Jul 18 '05 #2

P: n/a
On Thu, 9 Sep 2004 12:07:42 -0700 Darren wrote:
ip = socket.gethostbyaddr(socket.gethostname())
# returns: ('hostname.domain', ['hostname'], ['192.168.0.2'])
ip = str(ip[2])
# returns: "['192.168.0.2']"
ip = ip[2:-2]
# returns: '192.168.0.2'

This works fine, but seems a little crufty and convoluted, and usually
when things get crufty it means there is a better way to do it...so is
there a preferred way of obtaining a local IP address?


When functions return well documented results, I tend to just code like
this:

# returns: ('hostname.domain', ['hostname'], ['192.168.0.2'])
ip = socket.gethostbyaddr(socket.gethostname())[-1][0]

Gets the only list item of the last tuple item -- no calls to str() or
getting cute with the list's "[...]"

Pierre
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.