[Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0

更新内容:

1.增加内存信息获取

2.增加电池信息获取

3.增加磁盘信息获取

4.重新布局窗体

5.增加窗体名称

6.增加连接成功之前,不可按压

‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
>>文件: ps_server.py
>>作者: liu yang
>>邮箱: [email protected]
>>博客: www.cnblogs.com/liu66blog

‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os
from socket import *
import  psutil
from time import sleep

‘‘‘得到cpu使用率‘‘‘
‘‘‘返回值:(float)cpu使用率‘‘‘
def get_cpu_percent():
    cpu_start_alltime=psutil.cpu_times()
    ‘‘‘print(cpu_start_alltime)得到的内容:scputimes(user=14088.359375, system=20540.59375,
                            idle=362071.6875, interrupt=847.9375019073486, dpc=3795.6875)‘‘‘
    t1all=sum(cpu_start_alltime)#开始的cpu时间总和
    sleep(1)#休眠一秒
    cpu_end_alltime=psutil.cpu_times()
    t2all=sum(cpu_end_alltime)#结束时的cpu时间总和
    t1busy=t1all-cpu_start_alltime.idle#开始的忙碌时间=总时间-空闲时间
    t2busy=t2all-cpu_end_alltime.idle
    ‘‘‘cpu使用率:(结束的忙碌时间-开始的忙碌时间)/(结束的总时间-开始的总时间)‘‘‘
    cpu_busy=(t2busy-t1busy)/(t2all-t1all)*100
    # print("cpu使用率:%0.2f"%cpu_busy+‘%‘)
    return  cpu_busy
# 另一种写法
# def get_cpu_percent():
#     return psutil.cpu_percent(1)

‘‘‘将bytes数转换成更加直观的符号显示‘‘‘
‘‘‘返回值:(str)两位浮点数+B‘‘‘
def bytes2symbols(bytes_value):
    ‘‘‘单位符号‘‘‘
    symbols = (‘K‘ ,‘M‘, ‘G‘, ‘T‘, ‘P‘, ‘E‘, ‘Z‘, ‘Y‘)
    ‘‘‘每个单位对应的bytes数的字典,先定义为空‘‘‘
    prefix={}
    ‘‘‘要得到这样的{K:1024, M:1024*1024,G:1024**3},进行for循环‘‘‘
    for i,s in enumerate(symbols):
        # ‘‘‘enumerate()为列举函数‘‘‘
        # ‘‘‘i:代表下标‘‘‘
        # ‘‘‘s:代表改下标对应的值‘‘‘
        # ‘‘‘取到符号元组的值,作为prfix字典的key,根据key给value进行赋值‘‘‘
        prefix[s]=1024**(i+1)
    # ‘‘‘打印得到的对应字典‘‘‘
    # print(prefix)
    symbols_value=0
    symbol=‘‘
    # ‘‘‘循环prefix字典,得到转换值‘‘‘
    for key,value in prefix.items():
        if bytes_value >=value :
            symbols_value=bytes_value/value
            symbol=key
        # 如果不满足最小的KB,则以B显示
        elif bytes_value <1024:
            return  ‘%0.2fB‘%bytes_value
        # ‘‘‘返回转换值(str)‘‘‘
    return  ‘%0.2f%sB‘%(symbols_value,symbol)

‘‘‘得到内存信息‘‘‘
‘‘‘返回值:(str)内存信息‘‘‘
def get_memory():
    nt=psutil.virtual_memory()
    # 得到的数据:svmem(total=8482263040, available=1770676224, percent=79.1, used=6711586816, free=1770676224)
    # print(nt)
    total=bytes2symbols(nt.total)
    available=bytes2symbols(nt.available)
    percent=bytes2symbols(nt.percent)
    used=bytes2symbols(nt.used)
    free=bytes2symbols(nt.free)
    return ("总内存:%4s  \n可使用:%4s  \n已使用:%4s"%(total,available,used))

‘‘‘得到电池信息‘‘‘
‘‘‘返回值:(str)电池信息‘‘‘
def get_battery():
    ba=psutil.sensors_battery()
    # 未充电:sbattery(percent=13, secsleft=4294967295, power_plugged=False)
    # 在充电:sbattery(percent=14, secsleft= < BatteryTime.POWER_TIME_UNLIMITED: -2 >, power_plugged = True)
    battery=ba.percent
    if ba.power_plugged:
        power="正在充电中..."
    else:
        power="未充电"
    return (‘电量剩余:%d  电源状态:%2s‘%(battery,power))

‘‘‘磁盘分区信息显示‘‘‘
‘‘‘返回值:(str)磁盘信息‘‘‘
def get_disk():
    # 得到磁盘详细信息的列表
    disks = psutil.disk_partitions()
    # 磁盘信息列表
    disk_list = []
    disk_str=‘‘
    # 循环列表,打印
    for disk in disks:
        disk_list.append(‘disk:%2s type:%4s opts:%8s \n‘ % (disk.device, disk.fstype, disk.opts))
        disk_str+=‘disk:%2s type:%4s opts:%8s \n‘ % (disk.device, disk.fstype, disk.opts)
    # print(disk_str)
    return disk_str

# 在这里定义一个字典,存储字符和对应函数关系
command_dict={‘cpu‘:get_cpu_percent,‘memory‘:get_memory,‘battery‘:get_battery,‘disk‘:get_disk,}

# 创建服务端 TCP/IP IPv4
server=socket(AF_INET,SOCK_STREAM)
server.bind((‘localhost‘,66))
server.listen(5)
print("等待客户端进行连接...")

while True:
    # 等待连接阻塞
    conn,addr=server.accept()
    # 元祖取值需要用方括号!!!
    print("%s已连接..."%addr[0])
    while True:
        data = conn.recv(1024).decode(‘utf-8‘)
        if data in command_dict.keys():
            print(command_dict[data]())
            buf=str(command_dict[data]()).encode(‘utf-8‘)
            print(buf.decode())
            conn.send(buf)
        if not data:
            print("客户按可能已断开连接...")
            break
    conn.close()
conn.close()

原文地址:https://www.cnblogs.com/liu66blog/p/8452102.html

时间: 2024-11-10 05:04:08

[Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0的相关文章

[Python Study Notes]CS架构远程访问获取信息--Client端v2.0

更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 效果图: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] &

[Python Study Notes]CS架构远程访问获取信息--SERVER端

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_server.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]CS架构远程访问获取信息--Client端v1.0

更新内容: 1.添加entry栏默认ip和port口 2.修正退出功能 3.添加退出自动关闭窗口功能 4.优化cpu显示为固定保留两位小数 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] '''''''''

[Python Study Notes]CS架构远程访问获取信息--Client端

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

OEM信息自定义工具v2.0

支持win7系统属性OEM信息自定义修改 内置:联想,宏基,三星,惠普,戴尔,华硕,明基,方正,同方,东芝,康柏,富士通,LG,索尼,IBM,,Alienware,Tginkapad 这些OEM相关.参照 win7_oem信息修改工具v1.0制作,有所调整,界面更简洁. 表示对 dmqhcx88 年老痴呆 的感谢..自动判断系统位数,支持:Win7_86/64 OEM修改器.rar DIY,必备.... 绿野仙踪下载:http://pan.baidu.com/s/1c0oyXlI 备份下载:ht

[Python Study Notes]进程信息(丁丁软件监控进程,http-post)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 进程信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]内存信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 内存信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]电池信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 电池信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]cpu信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: cpu信息.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''