determining an assigned IP address

Mike Fletcher mfletch at tpresence.com
Tue Jun 27 12:35:23 EDT 2000


def getLocalHostIP( remote = ("www.yahoo.com", 80), forceCompute=0):
	'''Get the "public" address of the local machine, i.e.
	that address which is connected to the general internet.
	
	Code by Donn Cave, posted to comp.lang.python'''
	try:
		if not forceCompute:
			return LOCALHOST_PUBLIC_IP
	except NameError:
		pass
	import socket
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect( remote )
	ip, localport = s.getsockname()
	s.close()
	global LOCALHOST_PUBLIC_IP
	LOCALHOST_PUBLIC_IP = ip
	return ip

HTH,
Mike

-----Original Message-----
From: Jonathan Epstein [mailto:Jonathan_Epstein at nih.gov]
Sent: Monday, June 26, 2000 10:10 AM
To: python-list at python.org
Subject: Re: determining an assigned IP address


The following Perl code works:

  use Sys::Hostname;
  use Socket;

  $ip=gethostbyname(hostname());
  $theip=inet_ntoa($ip);


It should be relatively straightforward to convert this to Python.

- Jonathan


Bill Tolbert wrote:
> 
> I've just set up Linux with my cable modem and I'd like to use Python to
> help me launch remote X apps. I need to know the ip address assigned by
> my isp, but so far I'm striking out. gethostbyname_ex in the socket
> module returns the address I supplied when I thought it was static.
> Right now the only way I know to get the right address is to log into
> another machine and use who.
> 
> All help is appreciated!
> 
> Bill
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.
-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list