python获取电脑主机名和网卡物理地址
·
import uuid
import socket # 用于获取主机名
def get_mac_address():
# 获取 MAC 地址
mac_address = uuid.getnode()
# 将 MAC 地址转换为十六进制格式
mac_address_hex = ':'.join(("%012X" % mac_address)[i:i+2] for i in range(0, 12, 2))
return mac_address_hex
def get_hostname():
# 获取主机名
return socket.gethostname()
if __name__ == "__main__":
mac_address = get_mac_address()
hostname = get_hostname()
print("主机名:", hostname)
print("MAC 地址:", mac_address)
# 等待用户输入,保持控制台窗口不关闭
input("按下任意键关闭窗口...")
更多推荐
所有评论(0)