[Python Study Notes]CS架構遠端存取擷取資訊--SERVER端

來源:互聯網
上載者:User

標籤:decode   建立   usr   服務端   star   需要   bat   utf-8   soc   

‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘>>檔案: ps_server.py>>作者: liu yang>>郵箱: [email protected]‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘#!/usr/bin/env python# -*- coding: utf-8 -*-import sys, osfrom socket import *import  psutilfrom time import sleep# from cpu資訊 import cpu_message# from 記憶體資訊 import memory_message# from 電池資訊 import battery_message# from 磁碟資訊 import disk_partitions# from cpu使用率 import cpu_utilizationdef cpu_utilization():    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# 建立服務端 TCP/IP IPv4server=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 == ‘cpu‘:            buf=str(cpu_utilization()).encode(‘utf-8‘)            print(buf)            conn.send(buf)        if not data:            print("客戶按可能已中斷連線...")            break            conn.close()

[Python Study Notes]CS架構遠端存取擷取資訊--SERVER端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.