[Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0
Posted 刘六六
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0相关的知识,希望对你有一定的参考价值。
更新内容:
1.增加内存信息获取
2.增加电池信息获取
3.增加磁盘信息获取
4.重新布局窗体
5.增加窗体名称
6.增加连接成功之前,不可按压
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\' >>文件: ps_server.py >>作者: liu yang >>邮箱: liuyang0001@outlook.com >>博客: 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()
以上是关于[Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0的主要内容,如果未能解决你的问题,请参考以下文章
[Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0
[Python Study Notes]CS架构远程访问获取信息--Client端v2.0