C++ 获取ip地址 python获取ip地址

C++获取本机IP地址

/*
 *功能:传入一个网口名,获取相应的IP地址
 * */
 
 
#include <stdio.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
 
 
 string interface2ip(char *infName) {
        struct ifaddrs *ifAddrStruct;
        void *tmpAddrPtr=NULL;
        char ip[16];
        memset(ip, 0, sizeof(ip));
        static char infIP[16];
        getifaddrs(&ifAddrStruct);
        while (ifAddrStruct != NULL)
        {
        	if (ifAddrStruct->ifa_addr->sa_family==AF_INET && 0 == strcmp(ifAddrStruct->ifa_name,infName))
        	{
                tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
                inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN);
                //printf("eth0 ip :%s\n",ip);
                strcpy(infIP,ip);
                //printf("infIP is :%s\n",infIP);
        	}
            ifAddrStruct=ifAddrStruct->ifa_next;
        }
        //free ifaddrs
        freeifaddrs(ifAddrStruct);
        //return 0;
        return infIP;
}
 
 
int main()
{
	char infName[] = "eth0";
	printf("the result is : %s\n",interface2ip(infName));
 
 
    return 0;
}

python获取ip地址

import psutil
print(psutil.net_if_addrs()['eth0'][0].address)
posted @ 2022-08-12 15:02  hiccup_lh  阅读(127)  评论(0编辑  收藏  举报