首先就是需要引入头文件:引入socket

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import socket
#获取主机名
hostname = socket.gethostname()
print(hostname)
#获取ip地址
ip = socket.gethostbyname(hostname)
print(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)])

print(get_mac_address())