获取本地ip地址

import socket

def get_host_ip():
	try:
		s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		s.connect(('8.8.8.8', 80))
		ip = s.getsockname()[0]
	finally:
		s.close()
		
	return ip

print(get_host_ip())

获取本地IP地址

// Module: Sockets.build.cs

#include "SocketSubsystem.h"
#include "IPAddress.h"
#include "Interfaces/IPv4/IPv4Endpoint.h"

bool canBind = false;
TSharedRef<FInternetAddr> localIp = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr(*GLog, canBind);
FIPv4Endpoint LocalEndpoint = FIPv4Endpoint(localIp);
LocalIP = LocalEndpoint.Address.ToString();