python获取本机IP、mac地址、计算机名

获得本机MAC地址:

import uuid


def get_mac_address():
    mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
    return ":".join([mac[e:e + 2] for e in range(0, 11, 2)])


if __name__ == '__main__':
    print(get_mac_address())

获取本机电脑名和IP的方法:使用socket

import socket

# 获取本机电脑名
name = socket.getfqdn(socket.gethostname())
# 获取本机ip
ip = socket.gethostbyname(name)

print(name)
print(ip)
发布了328 篇原创文章 · 获赞 170 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/apollo_miracle/article/details/103748748