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

yipeiwu_com7年前Python基础

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

# -*- 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相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

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

相关文章

python2.7删除文件夹和删除文件代码实例

复制代码 代码如下:#!c:\python27\python.exe# -*- coding: utf-8 -*- import osimport re from os import p...

python如何读取bin文件并下发串口

下面是实现代码 # coding:utf-8 import time, serial from struct import * import binascii file = ope...

将Emacs打造成强大的Python代码编辑工具

将Emacs打造成强大的Python代码编辑工具

基本配置 Emacs本身提供了python-mode,输入M-x python-mode,就可以进入python模式。相应地,会在菜单栏出现Python菜单。当然,一般来讲,如果是.py...

利用Python暴力破解zip文件口令的方法详解

利用Python暴力破解zip文件口令的方法详解

前言 通过Python内置的zipfile模块实现对zip文件的解压,加点料完成口令破解 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的cla...

Python保存MongoDB上的文件到本地的方法

本文实例讲述了Python保存MongoDB上的文件到本地的方法。分享给大家供大家参考,具体如下: MongoDB上的文档通过GridFS来操作,Python也可以通过pymongo连接...