def _get_local_ip ( self ) : try : sock = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) # Use Google Public DNS server to determine own IP sock . connect ( ( '8.8.8.8' , 80 ) ) return sock . getsockname ( ) [ 0 ] except socket . error : try : return socket . gethostbyname ( socket . gethostname ( ) ) except socket . gaierror : return '127.0.0.1' finally : sock . close ( )
python get the local hostname
Try to determine the local IP address of the machine .
def _get_local_ip(self):
        """Try to determine the local IP address of the machine."""
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

            # Use Google Public DNS server to determine own IP
            sock.connect(('8.8.8.8', 80))

            return sock.getsockname()[0]
        except socket.error:
            try:
                return socket.gethostbyname(socket.gethostname())
            except socket.gaierror:
                return '127.0.0.1'
        finally:
            sock.close()