1. 程式人生 > >[ PyQt入門教程 ] PyQt+socket實現遠端操作伺服器

[ PyQt入門教程 ] PyQt+socket實現遠端操作伺服器

  來需求了。。幹活啦。。

需求內容

  部分時候由於快取重新整理、驗證碼顯示不出來或者瀏覽器打不開或者開啟速度很慢等原因,導致部分測試同事不想使用瀏覽器登入伺服器執行命令。期望有小工具可以替代登入瀏覽器的操作,直接傳送指令到伺服器執行並將執行結果返回。

需求設計

  1、開發介面,方便使用者輸入IP、使用者名稱、密碼以及執行的命令。

  2、IP、使用者名稱、密碼和命令輸入提供預設值。特別是使用者名稱和密碼,對於測試伺服器來說,通常都是固定的。

  3、IP、命令列輸入框可以自動補全使用者輸入。自動補全常用IP、命令列可以提高操作效率。

  4、可以自動儲存使用者執行成功的IP、命令列。用於完善自動補全命令(本文程式碼未實現)。

需求設計

  1、使用Qt Designer實現介面開發。開發後介面參考如下:

  2、使用socket程式登入伺服器並執行命令,並將結果顯示在介面文字框中。

程式碼實現(程式可以直接複製執行)

 1、使用Qt Designer實現介面開發。拖動4個label+4個輸入框+1個按鈕+1個textBrowser到主介面。開發後介面同需求設計中的截圖。

 2、使用pyuic5 -o commandtools.py commandtools.ui指令將.ui檔案轉換成.py檔案。

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'commandTools.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(483, 347)
        self.ip_label = QtWidgets.QLabel(Form)
        self.ip_label.setGeometry(QtCore.QRect(30, 20, 16, 16))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.ip_label.setFont(font)
        self.ip_label.setObjectName("ip_label")
        self.ip_lineEdit = QtWidgets.QLineEdit(Form)
        self.ip_lineEdit.setGeometry(QtCore.QRect(50, 20, 101, 20))
        self.ip_lineEdit.setObjectName("ip_lineEdit")
        self.username_label = QtWidgets.QLabel(Form)
        self.username_label.setGeometry(QtCore.QRect(160, 20, 61, 16))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.username_label.setFont(font)
        self.username_label.setObjectName("username_label")
        self.username_lineEdit = QtWidgets.QLineEdit(Form)
        self.username_lineEdit.setGeometry(QtCore.QRect(220, 20, 71, 20))
        self.username_lineEdit.setObjectName("username_lineEdit")
        self.password_label = QtWidgets.QLabel(Form)
        self.password_label.setGeometry(QtCore.QRect(300, 20, 61, 16))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.password_label.setFont(font)
        self.password_label.setObjectName("password_label")
        self.password_lineEdit = QtWidgets.QLineEdit(Form)
        self.password_lineEdit.setGeometry(QtCore.QRect(360, 20, 80, 20))
        self.password_lineEdit.setObjectName("password_lineEdit")
        self.command_label = QtWidgets.QLabel(Form)
        self.command_label.setGeometry(QtCore.QRect(30, 70, 51, 16))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.command_label.setFont(font)
        self.command_label.setObjectName("command_label")
        self.command_lineEdit = QtWidgets.QLineEdit(Form)
        self.command_lineEdit.setGeometry(QtCore.QRect(90, 70, 251, 20))
        self.command_lineEdit.setObjectName("command_lineEdit")
        self.result_textBrowser = QtWidgets.QTextBrowser(Form)
        self.result_textBrowser.setGeometry(QtCore.QRect(30, 120, 410, 201))
        self.result_textBrowser.setObjectName("result_textBrowser")
        self.run_Button = QtWidgets.QPushButton(Form)
        self.run_Button.setGeometry(QtCore.QRect(360, 70, 80, 23))
        self.run_Button.setObjectName("run_Button")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "cmdTool"))
        self.ip_label.setText(_translate("Form", "IP"))
        self.ip_lineEdit.setText(_translate("Form", "127.0.0.1"))
        self.username_label.setText(_translate("Form", "username"))
        self.username_lineEdit.setText(_translate("Form", "admin"))
        self.password_label.setText(_translate("Form", "password"))
        self.password_lineEdit.setText(_translate("Form", "Winovs12!"))
        self.command_label.setText(_translate("Form", "Command"))
        self.command_lineEdit.setText(_translate("Form", "LST LOG"))
        self.run_Button.setText(_translate("Form", "Run"))

3、實現主程式callcommand.py呼叫(業務與邏輯分離)。程式碼如下:

# -*- coding: utf-8 -*-

import sys
import time
import socket
from PyQt5.QtWidgets import QApplication, QMainWindow,QCompleter
from PyQt5.QtCore import Qt,QThread,pyqtSignal
from commandTools import Ui_Form


class MyMainForm(QMainWindow, Ui_Form):
    def __init__(self, parent=None):
        """
        建構函式
        """
        super(MyMainForm, self).__init__(parent)
        self.setupUi(self)
        self.run_Button.clicked.connect(self.execte_command)
        self.ip_init_lst = ['121.1.1.1', '192.168.1.1', '172.16.1.1']
        self.init_lineedit(self.ip_lineEdit,self.ip_init_lst)
        self.cmd_init_lst = ['LST LOG', 'LST PARA','MOD PARA']
        self.init_lineedit(self.command_lineEdit,self.cmd_init_lst)

    def init_lineedit(self, lineedit, item_list):
        """
        使用者初始化控制元件自動補全功能
        """
        # 增加自動補全
        self.completer = QCompleter(item_list)
        # 設定匹配模式  有三種: Qt.MatchStartsWith 開頭匹配(預設)  Qt.MatchContains 內容匹配  Qt.MatchEndsWith 結尾匹配
        self.completer.setFilterMode(Qt.MatchContains)
        # 設定補全模式  有三種: QCompleter.PopupCompletion(預設)  QCompleter.InlineCompletion   QCompleter.UnfilteredPopupCompletion
        self.completer.setCompletionMode(QCompleter.PopupCompletion)
        # 給lineedit設定補全器
        lineedit.setCompleter(self.completer)

    def execte_command(self):
        """
        登入伺服器,並執行命令
        """
        ip, username, password, command = self.get_input_para()
        print(type(ip))
        sockethandle = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        sockethandle.connect((str(ip), 6000))
        send_cmd = "username: %s, admin: %s, command: %s" % (username, password, command)
        print(send_cmd)
        sockethandle.sendall(send_cmd.encode('utf-8'))
        time.sleep(0.5)
        recdata = sockethandle.recv(65535)
        tran_recdata = recdata.decode('utf-8')
        self.result_textBrowser.setText(tran_recdata)

    def get_input_para(self):
        """
        獲取使用者介面輸入
        """
        ip = self.ip_lineEdit.text()
        username = self.username_lineEdit.text()
        password = self.password_lineEdit.text()
        command = self.command_lineEdit.text()
        return ip, username, password, command


if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWin = MyMainForm()
    myWin.show()
    sys.exit(app.exec_())

4、使用pyinstaller轉換成可執行的.exe檔案。命令:pyinstaller -F callcommand.py -w

執行成功,生成的檔案在d:\temp\dist\dist\callcommand.exe

5、執行callcommand.exe,點選run執行

關鍵程式碼

  1、輸入框自動補全功能函式。同樣適用於下拉框控制元件。

    def init_lineedit(self, lineedit, item_list):
        """
        使用者初始化控制元件自動補全功能
        """
        # 增加自動補全
        self.completer = QCompleter(item_list)
        # 設定匹配模式  有三種: Qt.MatchStartsWith 開頭匹配(預設)  Qt.MatchContains 內容匹配  Qt.MatchEndsWith 結尾匹配
        self.completer.setFilterMode(Qt.MatchContains)
        # 設定補全模式  有三種: QCompleter.PopupCompletion(預設)  QCompleter.InlineCompletion   QCompleter.UnfilteredPopupCompletion
        self.completer.setCompletionMode(QCompleter.PopupCompletion)
        # 給lineedit設定補全器
        lineedit.setCompleter(self.completer)

 2、socket中sendall函式要將命令使用utf-8編碼,否則會導致介面卡住:

sockethandle.sendall(send_cmd.encode('utf-8'))

3、需要將命令返回的內容解碼再寫入文字框,否則會導致介面卡住。

 recdata = sockethandle.recv(65535)
 tran_recdata = recdata.decode('utf-8')
 self.result_textBrowser.setText(tran_recdata)

附錄

  由於本地沒有伺服器用於除錯程式。所以使用socket搭建1個建議伺服器。伺服器功能實現將接收的命令原樣返回。就是接收什麼命令就給客戶端返回什麼內容。伺服器IP為本地IP127.0.0.1,繫結埠為6000。程式碼如下:

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

import socket
import sys

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print("socket create success!")
try:
    s.bind(('127.0.0.1',6000))
except socket.error as msg:
    print(msg)
    sys.exit(1)
s.listen(10)

while True:
    conn, addr = s.accept()
    print("success")
    data = conn.recv(65535)
    conn.sendall(data.decode('utf-8'))
conn.close()
s.close()

啟動伺服器:

簡陋的有點過分,但是滿足除錯需求了。。。

小結

  這個python+scoket需求實現的遠端登入伺服器執行命令只是把基本功能實現了。中間遇到的介面無響應甚至退出的問題(就是socket傳送和接收內容編解碼導致的)。。但是還有很多地方需要優化,比如對入參的判斷並輸出到文字框提示、對連線伺服器結果的判斷,還有介面的美化等內容。。正是這些小需求及實踐過程中遇到問題、解決問題的過程逐步提升編碼能力。。figh