【NanoPi NEO Plus2试用体验】5、显示系统信息

  • new world
  • LV5工程师
  • |      2017-08-14 23:39:44
  • 浏览量 1019
  • 回复:0
在上一节中我们已经完成显示我们的名字。这一节将会继续完善我们的系统信息显示 1)这些系统的实现,是我参考了树莓派的代码,将树莓派的代码移到Nano Pi中。 我们将要显示系统的版本,时间、ip、内存使用的情况和CPU的使用率和温度,这都是我们要实现的内容 2)函数的讲解: os 模块:python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块。 os.popen:调用系统函数,并将返回值进行保存 ip route show:为ip地址的扫描 3)代码的编写
# !/usr/bin/pthon3

import os

import time



#get CPU temp

def getCPUtemp():

    f=open("/sys/class/thermal/thermal_zone0/temp","r")

    temp = float(int(f.read())/1000.0)

    return temp



#CPU usaged

def getCUPusage():

    p = os.popen("df -h /")

    i = 0

    while 1:

        i = i+1

        line = p.readline()

        if i == 2:

              return (line.split())





#system name

def getSystemName():

    name = os.popen("hostnamectl")

    i = 0

    while 1:

        i = i+1

        line = name.readline()

        if i == 5:

            return (line.split(':').replace("\n","").strip())



#get time

# 年-月-日  时-分-秒 时区

def gettime():

    tim = time.strftime("%Y-%m-%d %H:%M:%S %Z",time.localtime())

    return (tim)



#获取CPU的使用率

def getCPUuse():

    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))



#get ip adress

def GetIpadress():

    s = os.popen("ip route show")

    s1 = s.readlines()

    h = ''

    for i in range(int(len(s1) / 2)):

        s2 = s1.split(' ')

        h = h + s2+'; '

    return h





#mian()



if __name__ == '__main__':

    ram = getCUPusage()

    ram_total = ram

    ram_used = ram

    ram_free = ram

    ram_per = ram

    cpu_usage = getCPUuse()



    print("System: {}".format(getSystemName()))

    print("time: {}".format(gettime()))

    print("IP : {}".format(GetIpadress()))

    print("CPU is used: {} %".format(cpu_usage))

    print("cpu temp is: {} °C".format(getCPUtemp()))

    print("RAM Used is used: {} of {}".format(ram_per,ram_total))



4)效果展示 我们可以看到,Nanopi已经完全的展示了它个各项指标,下一次我们就要将所有的功能加在一起了
  • 0
  • 收藏
  • 举报
  • 分享
我来回复

登录后可评论,请 登录注册

所有回答 数量:0
x
收藏成功!点击 我的收藏 查看收藏的全部帖子