1. 程式人生 > >Python獲取本機MAC地址,ip和主機名

Python獲取本機MAC地址,ip和主機名

獲取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地址和主機名:

import socket
pcname = socket.getfqdn(socket.gethostname( ))
pcip = socket.gethostbyname
(pcname) print(pcname) print(pcip)