def get_my_ip():
 import socket
 try:
  import Pyro4
  ns = Pyro4.naming.locateNS()
  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  s.connect((ns._pyroUri.host, ns._pyroUri.port))
  (result, port) = s.getsockname()
 except:
  try:
   import commands
   result = commands.getoutput('ifconfig').split('\n')[1].split()[1][5:]
   if (len(result.split('.')) != 4):
    raise Exception()
  except:
   result = socket.gethostbyname(socket.gethostname())
 return result

def get_my_ip ( ) : import socket try : import Pyro4 ns = Pyro4 . naming . locateNS ( ) s = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) s . connect ( ( ns . _pyroUri . host , ns . _pyroUri . port ) ) ( result , port ) = s . getsockname ( ) except : try : import commands result = commands . getoutput ( 'ifconfig' ) . split ( '\n' ) [ 1 ] . split ( ) [ 1 ] [ 5 : ] if ( len ( result . split ( '.' ) ) != 4 ) : raise Exception ( ) except : result = socket . gethostbyname ( socket . gethostname ( ) ) return result
try to obtain our external ip this tries to sidestep the issue of bogus /etc/hosts entries and other local misconfigurations .