天天看點

7.GUI程式設計-Qt Designer使用-端口掃描工具邏輯編寫.md

GUI程式設計-Qt Designer使用-端口掃描工具邏輯編寫及打包

  • 邏輯編寫
  • 打包釋出
    • 避免踩坑
  • 問題記錄:
#@File      :接口測試工具.py
#@Author    :gavin
#@Software  :PyCharm

import requests,threading,json

#1-導包QApplication:需要運作qt的gui程式,必須建立一個app對象
from PySide2.QtWidgets import QApplication
#2-導包QFile,需要打開一個ui檔案
from PySide2.QtCore import QFile
#3-py代碼需要加載ui檔案到記憶體中
from PySide2.QtUiTools import QUiLoader#需要加載你設計的ui檔案

class ApiTest():
    def __init__(self):
        # 5-打開這個檔案
        qFile = QFile('UI/接口測試工具.ui')  # 建立檔案對象
        qFile.open(QFile.ReadOnly)  # 隻讀方式
        # 6-加載對象--生成一個ui對象
        self.ui = QUiLoader().load(qFile)
        # 7-關閉Qfile
        qFile.close()
        # 發送請求
        self.ui.sendPushButton.clicked.connect(self.send_request)

    # 發送請求
    def send_request(self):
        #擷取method
        method =self.ui.methodComboBox.currentText()
        #擷取url
        url =self.ui.urlLineEdit.text()
        #擷取請求頭
        headers =self.ui.headerTextEdit.toPlainText()
        if headers.strip() !='':
            headers=json.load(headers)
        #擷取請求體
        payload = self.ui.bodyTextEdit.toPlainText()
        if payload.strip() !='':
            payload=json.load(payload)
        #構造發送請求對象
        req = requests.Request(method,url,headers=headers,json=payload)
        #建立一個會話
        s =requests.Session()
        #擷取請求前資料
        prepare =s.prepare_request(req)
        # 輸出請求資訊
        self.print_request(prepare)
        #非阻塞方式發送請求
        thrad =threading.Thread(target=self.thread_func,args=(s,prepare))
        thrad.start()
    #多線程發送請求
    def thread_func(self,s,prepare):
        resp=s.send(prepare)
        self.print_response(resp)
    # 列印請求資訊
    def print_request(self,req):
        if req.body==None:
            msg_body=""
        else:
            msg_body=req.body
        self.ui.textBrowser.append(
            '{}\n{}\n{}\n\n{}\n'.format(
                '-----------------------------請求資訊----------------------',
                req.method+" "+req.url,
                '\n'.join('{}:{}'.format(key,value) for key,value in req.headers.items()),
                msg_body
            )
        )


    def print_response(self,res):
        self.ui.textBrowser.append(
            '{}\nHTTP/1.1 {}\n{}\n\n{}\n'.format(
                '-----------------------------響應資訊----------------------',
                res.status_code,
                '\n'.join('{}:{}'.format(key,value) for key,value in res.headers.items()),
                res.text
            )
        )

if __name__ == '__main__':
    # 4-使用對象調用的方式去實作功能
    app = QApplication([])  # sys.argv 建立一個應用程式
    api =ApiTest()
    api.ui.show()
    app.exec_()
           

pyinstaller -F py檔案名 --noconsole --hidden-import PySide2.QtXml

F:壓縮打包,把所有環境打包在一起,直接給别人就可以用

--noconsole:運作時不展示cmd黑視窗,如果exe運作不起來,先不加此參數,運作可以在cmd中檢視報錯,cmd的報錯會一閃而過,可以通過錄屏,然後按幀來播放,定位到報錯資訊

-import PySide2.QtXml:導入動态包一起打包

  • 注意:
    • py檔案跟ui檔案需要跟工程目錄一樣對應,如果工程中py檔案和ui檔案同一個目錄,直接拷貝進去就可以使用
    • 如果ui檔案放在指定的目錄,ui檔案也要有一個相對路徑來存放ui檔案
  • 1、打包提示:PermissionError: [Errno 13] Permission denied: 'C:\Users\gavin\AppData\Local\pyinstaller\bincache00_py38_64bit\ucrtbase.dll'
Traceback (most recent call last):
  File "d:\soft\python3.8\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "d:\soft\python3.8\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "D:\soft\python3.8\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
  File "d:\soft\python3.8\lib\site-packages\PyInstaller\__main__.py", line 114, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "d:\soft\python3.8\lib\site-packages\PyInstaller\__main__.py", line 65, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "d:\soft\python3.8\lib\site-packages\PyInstaller\building\build_main.py", line 737, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "d:\soft\python3.8\lib\site-packages\PyInstaller\building\build_main.py", line 684, in build
    exec(code, spec_namespace)
  File "D:\py project\DevTools\GUI程式設計\接口測試工具.spec", line 21, in <module>
    exe = EXE(pyz,
  File "d:\soft\python3.8\lib\site-packages\PyInstaller\building\api.py", line 438, in __init__
    self.pkg = PKG(self.toc, cdict=kwargs.get('cdict', None),
  File "d:\soft\python3.8\lib\site-packages\PyInstaller\building\api.py", line 200, in __init__
    self.__postinit__()
  File "d:\soft\python3.8\lib\site-packages\PyInstaller\building\datastruct.py", line 160, in __postinit__
    self.assemble()
  File "d:\soft\python3.8\lib\site-packages\PyInstaller\building\api.py", line 262, in assemble
    fnm = checkCache(fnm, strip=self.strip_binaries,
  File "d:\soft\python3.8\lib\site-packages\PyInstaller\building\utils.py", line 284, in checkCache
    shutil.copy(fnm, cachedfile)
  File "d:\soft\python3.8\lib\shutil.py", line 415, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "d:\soft\python3.8\lib\shutil.py", line 261, in copyfile
    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\gavin\\AppData\\Local\\pyinstaller\\bincache00_py38_64bit\\ucrtbase.dll'

           
  • 2、原因分析:權限不允許,分析發現是防毒軟體不允許
  • 解決方法:關閉防毒軟體重新打包

    pyinstaller -F 接口測試工具.spec