我需要在软件运行的环境下获取信息。 python是否有用于此目的的库?
我想知道以下信息。
答案 0 :(得分:52)
其中一些可以从platform
模块获得:
>>> import platform
>>> platform.machine()
'x86'
>>> platform.version()
'5.1.2600'
>>> platform.platform()
'Windows-XP-5.1.2600-SP2'
>>> platform.uname()
('Windows', 'name', 'XP', '5.1.2600', 'x86', 'x86 Family 6 Model 15 Stepping 6, GenuineIntel')
>>> platform.system()
'Windows'
>>> platform.processor()
'x86 Family 6 Model 15 Stepping 6, GenuineIntel'
答案 1 :(得分:7)
os module具有uname功能,可以获取有关os& amp;的信息。版本:
>>> import os
>>> os.uname()
对于我的系统,使用2.6.18内核运行CentOS 5.4会返回:
('Linux','mycomputer.domain.user','2.6.18-92.1.22.el5PAE','#1 SMP Tue 12月16日12:36:25 EST 2008','i686')
答案 2 :(得分:4)
#Shamelessly combined from google and other stackoverflow like sites to form a single function
import platform,socket,re,uuid,json,psutil
def getSystemInfo():
try:
info={}
info['platform']=platform.system()
info['platform-release']=platform.release()
info['platform-version']=platform.version()
info['architecture']=platform.machine()
info['hostname']=socket.gethostname()
info['ip-address']=socket.gethostbyname(socket.gethostname())
info['mac-address']=':'.join(re.findall('..', '%012x' % uuid.getnode()))
info['processor']=platform.processor()
info['ram']=str(round(psutil.virtual_memory().total / (1024.0 **3)))+" GB"
return json.dumps(info)
except Exception as e:
logging.exception(e)
getSystemInfo()
Output sample:
'{
"platform": "Windows",
"platform-release": "10",
"platform-version": "10.0.18362",
"architecture": "AMD64",
"hostname": "test-user",
"ip-address": "192.168.31.147",
"mac-address": "xx:xx:xx:xx:xx:xx",
"processor": "Intel64 Family 6 Model 142 Stepping 10,GenuineIntel",
"ram": "8 GB"
}'
答案 3 :(得分:3)
找到了这个简单的代码
import platform
print("="*40, "System Information", "="*40)
uname = platform.uname()
print(f"System: {uname.system}")
print(f"Node Name: {uname.node}")
print(f"Release: {uname.release}")
print(f"Version: {uname.version}")
print(f"Machine: {uname.machine}")
print(f"Processor: {uname.processor}")