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:导入动态包一起打包

避免踩坑

打包完成会生成一个dist文件,把ui文件拷贝进去

  • 注意:
    • py文件跟ui文件需要跟工程目录一样对应,如果工程中py文件和ui文件同一个目录,直接拷贝进去就可以使用
    • 如果ui文件放在指定的目录,ui文件也要有一个相对路径来存放ui文件

qFile = QFile(‘UI/接口测试工具.ui‘) # 创建文件对象
7.GUI编程-Qt Designer使用-端口扫描工具逻辑编写.md

这个问题翻车了一天

问题记录:

  • 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

7.GUI编程-Qt Designer使用-端口扫描工具逻辑编写.md

上一篇:玩转CSS3(二)---CSS3实现瀑布布局


下一篇:IO流--FileInputStream--FileOutputStream