import socket def get_local_ip(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: # This does not send data, it just determines the active interface s.connect(("8.8.8.8", 80)) ip = s.getsockname()[0] finally: s.close() return ip if __name__ == "__main__": print("Local IP:", get_local_ip())