# 获取计算机名,常用的方法有三种
import os
import socket
 
# method one
name = socket.gethostname()
print(name)  # DESKTOP-S3BGVQN
 
# method two
name = os.popen("hostname").read()
print(name)  # DESKTOP-S3BGVQN
 
# method three
name = os.environ.get("COMPUTERNAME")
print(name)  # DESKTOP-S3BGVQN

# 获取ip地址也比较简单
import socket
# 但是要先获取计算机名
name = socket.gethostname()
# 然后通过计算机名获取ip
ip = socket.gethostbyname(name)
print(ip)  # 10.63.37.188
 
# 值得一提的是,不光能获取本机的ip,还可以获取其他服务器的ip。比如
print(socket.gethostbyname("www.baidu.com"))  # 220.181.112.244

# 获取mac
import uuid
addr_num = hex(uuid.getnode())[2:]
mac = "-".join(addr_num[i: i+2] for i in range(0, len(addr_num), 2))
print(mac)  # 48-5a-b6-ca-e2-9e
 
 
# 获取本机所有网卡的mac地址
from psutil import net_if_addrs
for k, v in net_if_addrs().items():
    for item in v:
        address = item[1]
        if "-" in address and len(address)==17:
            print(address)