Python3获取电脑IP、主机名、Mac地址的方法示例

时间:2022-05-20 13:03:27

本文实例讲述了python3获取电脑ip、主机名、mac地址的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding:utf-8 -*-
#! python3
'''
created on 2019年4月11日
@author: administrator
'''
import socket
import uuid
# 获取主机名
hostname = socket.gethostname()
#获取ip
ip = socket.gethostbyname(hostname)
# 获取mac地址
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)])
# iplist = socket.gethostbyname_ex(hostname)
# print(iplist)
print("主机名:",hostname)
print("ip:",ip)
print("mac地址:",get_mac_address())

运行结果:

主机名: snjckv1vqgb6np9
ip: 192.168.3.37
mac地址: e0:cb:4e:07:75:85

希望本文所述对大家python程序设计有所帮助。

原文链接:https://blog.csdn.net/lm_is_dc/article/details/80437053