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

來源:互聯網
上載者:User

標籤:width   host   isa   int   pad   bin   遠端存取   numbers   cli   

‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘>>檔案: ps_client.py>>作者: liu yang>>郵箱: [email protected]‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘#!/usr/bin/env python# -*- coding: utf-8 -*-import sys, osfrom 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.connButten=Button(self.root,text="串連",command=self.connect)        # 建立擷取按鈕        self.getButten=Button(self.root,text="擷取",state=‘disable‘,command=self.get_cpu_info)        # 建立斷開按鈕        self.exitButten=Button(self.root,text="退出",state=‘disable‘)        self.txtBox=Text(self.root,width=40,height=10)    def main(self):        self.et_ip.place(x=5,y=20)        self.et_port.place(x=5,y=50)        self.ip_lable.place(x=230,y=20)        self.port_lable.place(x=230,y=50)        self.connButten.place(x=5,y=80)        self.getButten.place(x=90,y=80)        self.exitButten.place(x=180,y=80)        self.txtBox.place(x=5,y=120)        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.exitButten[‘state‘]=‘active‘                self.getButten[‘state‘]=‘active‘    def get_cpu_info(self):        self.data=‘cpu‘        self.client.send(self.data.encode(‘utf-8‘))        cpu_used=self.client.recv(1024).decode(‘utf-8‘)        print(cpu_used)        self.txtBox.delete(0.0, END)        # 字串前加上r為防轉義        self.txtBox.insert(0.0, "當前的cpu使用率:%s"%cpu_used+r"%")    def exit_connect(self):        self.client.close()        self.txtBox.delete(0.0, END)        self.txtBox.insert(0.0, "當前串連已斷開...")if __name__ == ‘__main__‘:    Ps=Ps_client()    Ps.main()

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.