Python 获取本机IP地址

import socket
    #获取本机IP地址
        self.local_ip = socket.gethostbyname(socket.gethostname())
        print (self.local_ip)

  

python 判断本机是否联网

 1                 timeout = 200
 2                 host = ‘www.baidu.com‘
 3                 port = 80
 4                 s=socket.socket()
 5                 s.settimeout(timeout)
 6                 status = s.connect_ex((host,port))
 7                 if status == 0:
 8                     #Connect OK
 9                 else:
10                     print ‘ping error.‘    

Python 获取本机IP地址

时间: 2024-11-25 04:25:05

Python 获取本机IP地址的相关文章

python 获取本机ip地址的方法(Unix 平台)

1 #!/usr/bin/python 2 import socket 3 import fcntl 4 import struct 5 def get_ip_address(ifname): 6 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 7 return socket.inet_ntoa(fcntl.ioctl( 8 s.fileno(), 9 0x8915, # SIOCGIFADDR 10 struct.pack('256s'

[转] python 获取本机ip地址的两种实现方法

#!/usr/bin/python import socket import fcntl import struct def get_ip_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, # SIOCGIFADDR struct.pack('256s', ifname[:15]) )[20:2

获取本机IP地址

这里有两种方法: 1 //获取本机IP 2 - (NSString *)localIPAddress 3 { 4 NSString *localIP = nil; 5 struct ifaddrs *addrs; 6 if (getifaddrs(&addrs)==0) { 7 const struct ifaddrs *cursor = addrs; 8 while (cursor != NULL) { 9 if (cursor->ifa_addr->sa_family == AF_

关于是用dotnet获取本机IP地址+计算机名的方法

印象中在maxscript帮助文档里找到过方法,但是当时没记下来.只能通过dotnet实现了. 如果电脑有无线网卡和本地连接,可能会出现乱码,也问了写dotnet的朋友,提供了一些思路,不过最终还是使用了这个笨办法. fn getIP_PCname = ( cc = (dotnetclass "System.Net.Dns") oo = cc.GetHostAddresses(cc.GetHostName()) for ip = 1 to oo.count do ( getip = f

java获取本机IP地址

/** * WIFI没打开:ip为127.0.0.1 * 获取本机IP地址字符串 * @return */ public String getWifiIp() { if (!getWifiEnabled()) { return "127.0.0.1"; } WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = intToIp(i

Linux编程获取本机IP地址

使用函数getifaddrs来枚举网卡IP,其中使用到的结构体如下所示: struct ifaddrs { struct ifaddrs *ifa_next; /* Next item in list */ char *ifa_name; /* Name of interface */ unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */ struct sockaddr *ifa_addr; /* Address of interface *

#获取本机IP地址时排除IPv6类型,只返回IPv4地址的方法

public static string GetLocalIP(){try{string HostName = Dns.GetHostName(); //得到主机名IPHostEntry IpEntry = Dns.GetHostEntry(HostName); for (int i=0; i < IpEntry.AddressList.Length; i++){//从IP地址列表中筛选出IPv4类型的IP地址//AddressFamily.InterNetwork表示此IP为IPv4,//Ad

C# 获取本机IP地址以及转换字符串

/// <summary> /// IP地址转化 /// </summary> /// <param name="ipaddr">整型的IP地址</param> /// <returns>字符串的IP地址</returns> private string UintIPToStringIP(uint ipaddr) { string hexStr = ipaddr.ToString("X8");

Qt获取本机IP地址

Qt获取本机IP地址: Qt版本:4.8.6 #include <QtNetwork/QNetworkInterface.h> QString ipAddr; QList<QHostAddress> list = QNetworkInterface::allAddresses(); foreach (QHostAddress address, list) { if (address.protocol() == QAbstractSocket::IPv4Protocol) { ipA