是不是少了一节控制面板的教程?

来源:6-4 .控制面板页面搭建

老卢123123

2017-10-15

后台模板首页就是控制面板.

功能也很重要.

教程后台页面搭建也讲了.

但是功能实现怎么没有?

http://img1.sycdn.imooc.com/szimg/59e27f80000188e911690729.jpg

写回答

1回答

rustgopy

2017-10-15

同学,你好,涉及到系统层的知识,有一点升入,后面做课程升级会讲解,你可以先看下代码!

# coding:utf8
import psutil
import datetime


class Sinfo:
    # 1.系统登录用户
    @classmethod
    def users(cls):
        data = psutil.users()
        data = map(
            lambda v:
            dict(
                name=v.name,
                terminal=v.terminal,
                host=v.host,
                started=datetime.datetime.fromtimestamp(v.started).strftime("%Y-%m-%d %H:%M:%S")
            ), data)
        return data

    # 2.最近开机时间
    @classmethod
    def boot_time(cls):
        data = psutil.boot_time()
        data = dict(
            boot_time=datetime.datetime.fromtimestamp(data).strftime("%Y-%m-%d %H:%M:%S")
        )
        return data

    # 3.内存信息
    @classmethod
    def virtual_memory(cls):
        data = psutil.virtual_memory()
        data = dict(
            total=round(data.total / (1024 ** 3.0), 4),
            available=round(data.available / (1024 ** 3.0), 4),
            percent=data.percent,
            used=round(data.used / (1024 ** 3.0), 4),
            free=round(data.free / (1024 ** 3.0), 4),
            active=round(data.active / (1024 ** 3.0), 4),
            inactive=round(data.inactive / (1024 ** 3.0), 4),
            buffers=round(data.buffers / (1024 ** 3.0), 4),
            cached=round(data.cached / (1024 ** 3.0), 4),
            shared=round(data.shared / (1024 ** 3.0), 4),
        )
        return data

    # 4.交换分区信息
    @classmethod
    def swap_memory(cls):
        data = psutil.swap_memory()
        data = dict(
            total=round(data.total / (1024 ** 3.0), 4),
            used=round(data.used / (1024 ** 3.0), 4),
            free=round(data.free / (1024 ** 3.0), 4),
            percent=data.percent,
            sin=data.sin,
            sout=data.sout
        )
        return data

    # 5.网络信息
    @classmethod
    def net(cls):
        net_io_counters = psutil.net_io_counters(pernic=True)
        net_if_addrs = psutil.net_if_addrs()
        data = [
            dict(
                name=k,
                bytes_sent=v.bytes_sent,
                bytes_recv=v.bytes_recv,
                packets_sent=v.packets_sent,
                packets_recv=v.packets_recv,
                errin=v.errin,
                errout=v.errout,
                dropin=v.dropin,
                dropout=v.dropout,
                net_if_addr=map(
                    lambda v:
                    dict(
                        address=v.address,
                        netmask=v.netmask,
                        broadcast=v.broadcast,
                        family=v.family
                    ),
                    net_if_addrs[k]
                )
            )
            for k, v in net_io_counters.items()
        ]
        return data

    # 6.磁盘信息
    @classmethod
    def disk(cls):
        disk_partitions = psutil.disk_partitions()
        disk_io_counters = psutil.disk_io_counters(perdisk=False)
        data = dict(
            disk_partitions=map(
                lambda v: dict(
                    device=v.device,
                    mountpoint=v.mountpoint,
                    fstype=v.fstype,
                    opts=v.opts,
                    disk_usage=dict(
                        total=psutil.disk_usage(v.mountpoint).total,
                        used=psutil.disk_usage(v.mountpoint).used,
                        free=psutil.disk_usage(v.mountpoint).free,
                        percent=psutil.disk_usage(v.mountpoint).percent
                    )
                ),
                disk_partitions
            ),
            disk_io_counters=dict(
                read_count=disk_io_counters.read_count,
                write_count=disk_io_counters.write_count,
                read_bytes=disk_io_counters.read_bytes,
                write_bytes=disk_io_counters.write_bytes,
                read_time=datetime.datetime.fromtimestamp(disk_io_counters.read_time).strftime("%Y-%m-%d %H:%M:%S"),
                write_time=datetime.datetime.fromtimestamp(disk_io_counters.write_time).strftime("%Y-%m-%d %H:%M:%S"),
                read_merged_count=disk_io_counters.read_merged_count,
                write_merged_count=disk_io_counters.write_merged_count,
                busy_time=datetime.datetime.fromtimestamp(disk_io_counters.busy_time).strftime("%Y-%m-%d %H:%M:%S")
            )
        )
        return data

    # 7.进程信息
    @classmethod
    def pids(cls):
        data = []
        try:
            pids_list = psutil.pids()
            data = map(
                lambda v:
                dict(
                    name=v.name(),
                    pid=v.pid,
                    exe=v.exe(),
                    cwd=v.cwd(),
                    status=v.status(),
                    create_time=datetime.datetime.fromtimestamp(v.create_time()).strftime("%Y-%m-%d %H:%M:%S"),
                    memory_percent=v.memory_percent(),
                    num_threads=v.num_threads(),
                    username=v.username(),
                    terminal=v.terminal(),
                    cpu_num=v.cpu_num(),
                    io_counters=dict(
                        read_count=v.io_counters().read_count,
                        write_count=v.io_counters().write_count,
                        read_bytes=v.io_counters().read_bytes,
                        write_bytes=v.io_counters().write_bytes,
                    ),
                    is_running=v.is_running()
                ),
                map(
                    lambda v:
                    psutil.Process(v),
                    filter(
                        lambda val:
                        psutil.pid_exists(val),
                        pids_list
                    )
                )
            )
        except Exception, e:
            pass
        return data


1
1
老卢123123
非常感谢老师的回答!
2017-10-15
共1条回复

Python Flask 构建微电影视频网站

Python 最好用轻量级框架,让你轻松胜任 Python Web 工程师

683 学习 · 663 问题

查看课程