【Python学习】获取计算机hostname,ip,mac
1 import socket 2 import uuid 3 # 主机名 4 hostname = socket.gethostname() 5 #ip地址 6 ip = socket.gethostbyname(hostname) 7 #物理地址 8 mac = uuid.UUID(int=uuid.getnode()).hex[-12:] 9 mac = ":".join([mac[e:e + 2] for e in range(0, 11, 2)]) 10 11 def get_host_ip(): 12 13 """ 14 查询本机通信ip地址 15 :return: ip 16 """ 17 try: 18 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 19 s.connect(('8.8.8.8', 80)) 20 ip = s.getsockname()[0] 21 finally: 22 s.close() 23 return ip
作者:gtea
博客地址:https://www.cnblogs.com/gtea