[Python Study Notes] CS Architecture Remote Access get information--server End v2.0

Source: Internet
Author: User
Tags cpu usage

Update content:

1. Increased Memory information acquisition

2. Increase Battery Information acquisition

3. Increase disk Information acquisition

4. Re-layout the form

5. Add the form name

6. Do not press until the connection is successfully added


"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "", "" "". "" "" "" "" "" "" "" "," "" "" "," "" "" "," "" "," "" ">> Ps_server.py>> Author: Liu Yang>> e-mail: [email protected]>> blog: Www.cnblogs.com/liu66blog "" " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "". "" "" "" "" "" "" "" "" "" "" "" "#!/usr/bin/env python#"------ * * coding:utf-8-*-import sys, osfrom socket import *import psutilfrom time import sleep ' Get CPU usage ' ' return value: (float) CPU makes Use rate ' Def get_cpu_percent (): Cpu_start_alltime=psutil.cpu_times () ' Print (cpu_start_alltime) ' What it gets: 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# Start busy = total time-idle time T2busy=t2all-cpu_end_alltime.idle "C PU usage: (end of busy time-start busy time)/(total time to end-total time to start) ' cpu_busy= (T2BUSY-T1BUSY)/(T2all-t1all) *100 # print ("CPU Usage:%0.2f"%cpu_busy+ '% ') return cpu_busy# another notation # def get_cpu_percent () : # return Psutil.cpu_percent (1) "' converts bytes number to more intuitive symbol display ' ' ' Return value: (str) Two-bit floating-point +b ' def bytes2symbols (bytes_value): ' Single The bit sign ' ' symbols = (' K ', ' M ', ' G ', ' T ', ' P ', ' E ', ' Z ', ' Y ') ' Each unit corresponds to a dictionary of bytes number, defined first as null ' prefix={} ' to get such a {k:1 024, m:1024*1024,g:1024**3}, for Loop "' for i,s in Enumerate (symbols): # ' Enumerate () As enumeration function ' ' # ' I: Represents the next "' # ': Represents the value of the subscript corresponding to the value ' ' # ' taken to the values of the symbol tuple, as the key of the Prfix dictionary, assigns value to value according to key ' prefix[s]=1024** (i+1) # ' ' Print out the corresponding dictionary ' ' # Print (prefix) symbols_value=0 symbol= ' # ' Loop prefix dictionary, get converted value ' ' for Key,value in prefix.it EMS (): If Bytes_value >=value:symbols_value=bytes_value/value Symbol=key # If not meet the most  Small kb, then B shows elif bytes_value <1024:return '%0.2fb '%bytes_value # ' return conversion value (str) ' Return '%0.2F%SB '% (symbols_value, symbol) ' Get memory information ' ' Return value: (str) memory information ' Def get_memory (): Nt=psutil.virtual_memory () # Data obtained: 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=bytes2s Ymbols (Nt.free) return ("Total Memory:%4s \ n can be used:%4s \ n used:%4s"% (total,available,used)) ' ' Get Battery Information ' "' Return Value: (str) Battery info ' ' Def get _battery (): Ba=psutil.sensors_battery () No charge: Sbattery (percent=13, secsleft=4294967295, Power_plugged=false) # in charge Electricity: Sbattery (percent=14, secsleft= < batterytime.power_time_unlimited:-2;, power_plugged = True) battery=ba.perce NT if ba.power_plugged:power= "charging in ..." else:power= "not Charged" return (' Battery remaining:%d power state:%2s '% (battery,pow ER)) ' Disk partition information display ' ' ' "' Return Value: (str) disk info ' Def get_disk (): # Get a list of disk details disks = psutil.disk_partitions () # Disk Information List D Isk_list = [] disk_str= "    # loop list, print for disk in Disks:disk_list.append (' disk:%2s type:%4s opts:%8s \ n '% (Disk.device, Disk.fstype, D    isk.opts)) disk_str+= ' disk:%2s type:%4s opts:%8s \ n '% (Disk.device, Disk.fstype, disk.opts) # print (DISK_STR) Return disk_str# defines a dictionary here, storing characters and corresponding function relationships command_dict={' CPU ': get_cpu_percent, ' memory ': get_memory, ' battery ': get_ Battery, ' disk ': get_disk,}# Create server-side TCP/IP ipv4server=socket (af_inet,sock_stream) server.bind (' localhost ', 66)    Server.listen (5) print ("Waiting for the client to connect ...") while True: # waits for a connection to block Conn,addr=server.accept () # The tuple takes a value in square brackets!!! Print ("%s connected ..."%addr[0]) while true:data = Conn.recv (1024x768). Decode (' Utf-8 ') if data in Command_dict.keys (): Print (Command_dict[data] ()) Buf=str (Command_dict[data] ()). Encode (' Utf-8 ') print (BUF.D Ecode ()) Conn.send (BUF) if not data:print ("Customer may be disconnected ...") Break Conn.close () c Onn.close ()

[Python Study notes]cs schema remote access get information--server End v2.0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.