[Python Study Notes]CS架构远程访问获取信息--Client端

‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
>>文件: ps_client.py
>>作者: liu yang
>>邮箱: [email protected]

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

#!/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.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()

原文地址:https://www.cnblogs.com/liu66blog/p/8450347.html

时间: 2024-10-30 06:45:22

[Python Study Notes]CS架构远程访问获取信息--Client端的相关文章

[Python Study Notes]CS架构远程访问获取信息--Client端v2.0

更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 效果图: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] &

[Python Study Notes]CS架构远程访问获取信息--Client端v1.0

更新内容: 1.添加entry栏默认ip和port口 2.修正退出功能 3.添加退出自动关闭窗口功能 4.优化cpu显示为固定保留两位小数 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] '''''''''

[Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0

更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_server.py >>作者: liu yang >>邮箱: [email protected] >&g

[Python Study Notes]CS架构远程访问获取信息--SERVER端

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_server.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]批量将ppt转换为pdf

脚本使用: 1.python 3.x(2.x未测试) 2.需要机器上装有PowerPoint 3.pip install comtypes 4.将需要转的ppt,pptx格式文件放到当前目录下 5.运行该脚本等待即可 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ppt2pdf.py >>作者: liu yang &g

[Python Study Notes]进程信息(丁丁软件监控进程,http-post)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 进程信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]内存信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 内存信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]电池信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 电池信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]cpu信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: cpu信息.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''