1. 程式人生 > >[Python Study Notes]CS架構遠程訪問獲取信息--Client端v2.0

[Python Study Notes]CS架構遠程訪問獲取信息--Client端v2.0

itl 顯示 odin cli man 技術分享 名稱 架構 title

更新內容:

1.增加內存信息獲取

2.增加電池信息獲取

3.增加磁盤信息獲取

4.重新布局窗體

5.增加窗體名稱

6.增加連接成功之前,不可按壓

效果圖:

技術分享圖片

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

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

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

import sys, os
from socket import *
from tkinter import *

class Ps_client():
    def __init__(self):
        self.ip=None
        self.port=None
        self.data=None
        # 創建ipv4套接字
        self.client=socket(AF_INET,SOCK_STREAM)
        self.root=Tk()
        self.root.geometry(‘300x300+250+250‘)


        # 創建IP輸入框
        var_ip = StringVar()
        var_ip.set(‘localhost‘)
        self.et_ip=Entry(self.root,width=30,textvariable=var_ip)
        self.et_ip.pack()

        # 創建IP輸入框的Label
        self.ip_lable=Label(self.root,text="ip地址")

        # 創建端口號輸入框
        var_port = StringVar()
        var_port.set(66)
        self.et_port=Entry(self.root,width=30,textvariable=var_port)
        # 創建端口號Label
        self.port_lable=Label(self.root,text="端口號")

        # 創建連接按鈕,註意!!!command=後面的連接的不加括號
        self.connButton=Button(self.root,text="連接",command=self.connect)
        # 創建獲取cpu按鈕
        self.getCpuButton=Button(self.root,text="CPU",state=‘disable‘,command=self.get_cpu_info)
        # 創建獲取memory按鈕
        self.getMemoryButton=Button(self.root,text="內存",state=‘disable‘,command=self.get_memory_info)
        # 創建獲取battery按鈕
        self.getBatteryButton = Button(self.root, text="電池", state=‘disable‘, command=self.get_battery_info)
        # 創建獲取disk按鈕
        self.getDiskButton=Button(self.root,text="磁盤",state=‘disable‘,command=self.get_disk_info)
        # 創建斷開按鈕
        self.exitButton=Button(self.root,text="退出",state=‘disable‘,command=self.exit_connect)

        self.txtBox=Text(self.root,width=40,height=10)



    def main(self):
        self.root.title(‘博客園:liu66‘)
        self.et_ip.place(x=10,y=20)
        self.et_port.place(x=10,y=50)
        self.ip_lable.place(x=245,y=20)
        self.port_lable.place(x=245,y=50)

        self.connButton.place(x=10,y=80)
        self.getCpuButton.place(x=70,y=80)
        self.getMemoryButton.place(x=130,y=80)
        self.getBatteryButton.place(x=190,y=80)
        self.getDiskButton.place(x=250,y=80)
        self.txtBox.place(x=5,y=120)
        self.exitButton.place(x=255,y=260)
        # self.txtBox.insert(INSERT,‘在這裏顯示內容‘)
        self.root.mainloop()

    def connect(self):
        self.ip=self.et_ip.get()
        try:
            self.port=int(self.et_port.get())
        except ValueError:
            self.txtBox.delete(0.0,END)
            self.txtBox.insert(0.0,"請輸入合法的ip和端口...")
        else:
            print("ip:%s"%self.ip)
            print("port:%s"%self.port)
            self.txtBox.delete(0.0,END)
            self.txtBox.insert(0.0,"正在鏈接中...")
            try:
                self.client.connect((self.ip,self.port))
            except OSError:
                print("向一個無法連接的網絡嘗試了一個套接字操作")
                self.txtBox.delete(0.0, END)
                self.txtBox.insert(0.0, "%s:%d連接失敗..."%(self.ip,self.port))
            else:
                print("%s連接成功..."%self.ip)
                self.txtBox.delete(0.0, END)
                self.txtBox.insert(0.0, "%s:%d連接成功..."%(self.ip,self.port))
                # 連接成功則將其他按鈕變為可按狀態
                self.exitButton[‘state‘]=‘active‘
                self.getCpuButton[‘state‘]=‘active‘
                self.getMemoryButton[‘state‘]=‘active‘
                self.getBatteryButton[‘state‘]=‘active‘
                self.getDiskButton[‘state‘]=‘active‘


    def get_cpu_info(self):
        self.data=‘cpu‘
        self.client.send(self.data.encode(‘utf-8‘))
        # 將接受的數據裝換成浮點數據
        cpu_used=float(self.client.recv(1024).decode(‘utf-8‘))
        print(‘CPU使用率:%0.2f‘%cpu_used+‘%‘)
        self.txtBox.delete(0.0, END)
        # 字符串前加上r為防轉義
        self.txtBox.insert(0.0, "當前的cpu使用率:%0.2f"%cpu_used+r"%")

    def get_memory_info(self):
        self.data=‘memory‘
        self.client.send(self.data.encode(‘utf-8‘))
        memory_message=self.client.recv(1024).decode(‘utf-8‘)
        print(memory_message)
        # 清除顯示
        self.txtBox.delete(0.0, END)
        # 顯示內存信息
        self.txtBox.insert(0.0, "%s" %memory_message)


    def get_battery_info(self):
        self.data=‘battery‘
        self.client.send(self.data.encode(‘utf-8‘))
        battery_message=self.client.recv(1024).decode(‘utf-8‘)
        print(battery_message)
        # 清除顯示
        self.txtBox.delete(0.0, END)
        # 顯示內存信息
        self.txtBox.insert(0.0, "%s" %battery_message)

    def get_disk_info(self):
        self.data=‘disk‘
        self.client.send(self.data.encode(‘utf-8‘))
        disk_message=self.client.recv(1024).decode(‘utf-8‘)
        print(disk_message)
        # 清除顯示
        self.txtBox.delete(0.0, END)
        # 顯示內存信息
        self.txtBox.insert(0.0, "%s" %disk_message)


    def exit_connect(self):
        self.client.close()
        self.txtBox.delete(0.0, END)
        self.txtBox.insert(0.0, "當前連接已斷開...")
        print("當前連接已斷開...")
        self.exitButton[‘state‘] = ‘disable‘
        self.getCpuButton[‘state‘] = ‘disable‘
        self.getMemoryButton[‘state‘] = ‘disable‘
        # 關閉當前窗口
        self.root.destroy()

if __name__ == ‘__main__‘:
    Ps=Ps_client()
    Ps.main()

[Python Study Notes]CS架構遠程訪問獲取信息--Client端v2.0