pyqt5实现http接口测试工具

该博客介绍了如何使用PyQt5的QTDesigner创建一个GUI界面,该界面包含请求头、请求方式、URL、请求体和响应体等元素。然后通过pyuic5转换为Python代码,并在http_main.py中解耦UI代码,实现了按钮点击事件的绑定,包括执行HTTP请求和清除功能。示例代码展示了如何处理GET和POST请求,并将响应结果显示在界面上。
摘要由CSDN通过智能技术生成

1、首先通过QT designer实现如下界面

2、 通过命令转python代码

命令:pyuic5 -x -o http_req.py httpreq.ui

(http_req.py:是通过http_req.ui生成的python代码文件, http_req.ui,:通过QT designer设计的界面代码)

http_req.py代码如下:

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

# Form implementation generated from reading ui file 'http_req.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(960, 749)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        #请求头
        # self.hea = ['token','content-type']
        # self.listWidget = QtWidgets.QListWidget(self.centralwidget)
        # self.listWidget.setGeometry(QtCore.QRect(10, 140, 931, 121))
        # self.listWidget.addItems(self.hea)
        # self.listWidget.setObjectName("listWidget")

        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(40, 150, 54, 12))
        self.label_4.setObjectName("label_4")
        # self.horizontalLayout_3.addWidget(self.label_4)

        self.token = QtWidgets.QLineEdit(self.centralwidget)
        self.token.setObjectName("token")
        self.token.setGeometry(QtCore.QRect(150, 150, 781, 21))
        # self.horizontalLayout_3.addWidget(self.token)

        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(30, 190, 71, 16))
        self.label_5.setObjectName("label_5")
        # self.horizontalLayout_3.addWidget(self.label_5)

        self.content_type = QtWidgets.QLineEdit(self.centralwidget)
        self.content_type.setObjectName("content_type",)
        self.content_type.setGeometry(QtCore.QRect(150, 190, 781, 21))
        # self.horizontalLayout_3.addWidget(self.content_type)

        #标签1
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20, 120, 54, 12))
        self.label.setObjectName("label")
        #标签2
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(20, 280, 54, 12))
        self.label_2.setObjectName("label_2")
        #标签3
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(490, 280, 54, 12))
        self.label_3.setObjectName("label_3")

        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(30, 0, 861, 81))
        self.widget.setObjectName("widget")
        #布局
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        #请求方式
        self.comboBox = QtWidgets.QComboBox(self.widget)
        self.comboBox.setObjectName("comboBox")
        self.horizontalLayout.addWidget(self.comboBox)
        self.comboBox.addItems(['get','post'])
        self.comboBox.setCurrentIndex(0)
        #请求URl
        self.url = QtWidgets.QLineEdit(self.widget)
        self.url.setObjectName("url")
        self.horizontalLayout.addWidget(self.url)

        #执行
        self.pushButton = QtWidgets.QPushButton(self.widget)
        self.pushButton.setAutoRepeatInterval(100)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        #清除
        self.pushButton_2 = QtWidgets.QPushButton(self.widget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout.addWidget(self.pushButton_2)

        self.widget1 = QtWidgets.QWidget(self.centralwidget)
        self.widget1.setGeometry(QtCore.QRect(10, 300, 931, 401))
        self.widget1.setObjectName("widget1")

        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget1)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        #请求体
        self.textEdit = QtWidgets.QTextEdit(self.widget1)
        self.textEdit.setObjectName("textEdit")
        self.horizontalLayout_2.addWidget(self.textEdit)
        #响应体
        self.textEdit_2 = QtWidgets.QTextEdit(self.widget1)
        self.textEdit_2.setObjectName("textEdit_2")
        self.textEdit_2.setReadOnly(True)
        self.horizontalLayout_2.addWidget(self.textEdit_2)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 960, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "请求头"))
        self.label_2.setText(_translate("MainWindow", "请求体"))
        self.label_3.setText(_translate("MainWindow", "响应体"))
        self.label_4.setText(_translate("MainWindow", "token"))
        self.label_5.setText(_translate("MainWindow", "content_type"))
        self.pushButton.setText(_translate("MainWindow", "执行"))
        self.pushButton_2.setText(_translate("MainWindow", "清除"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

3、创建一个脚本,用于解耦ui中代码

http_main。py,代码如下:

import json

import requests
import urllib3
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
import sys
import http_req

class Myclass(object):
    def __init__(self):
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        self.ui = http_req.Ui_MainWindow()
        self.ui.setupUi(MainWindow)

        #绑定pushButton_2_clicked清除方法
        self.ui.pushButton_2.clicked.connect(self.pushButton_2_clicked)
        #绑定请求方法
        self.ui.pushButton.clicked.connect(self.get_req)

        MainWindow.show()
        sys.exit(app.exec_())


    def get_req(self):
        #获取url
        req_url = self.ui.url.text()
        #获取token
        req_token = self.ui.token.text()
        #获取content_type
        req_content_type = self.ui.content_type.text()
        header = {"token":req_token,"content-type":req_content_type}
        #获取data
        req_data = self.ui.textEdit.toPlainText()
        #去掉\
        # req_data = req_data.replace("\\","")
        # req_datas=json.dumps(req_data)
        #获取请求方式
        req = self.ui.comboBox.currentText()
        print(req)
        if req == 'get':
            response = requests.request(url=req_url,method="get",headers=header,data=req_data,verify=False)
        else:
            response = requests.request(url=req_url,method="post",data=req_data,headers=header,verify=False)
        #将返回值打印到textEdit_2中
        self.ui.textEdit_2.setPlainText(str(response.text))

    def pushButton_2_clicked(self):
        url = self.ui.url.clear()
        print(url)


if __name__=="__main__":
    Myclass()

好的,那么让我们开始吧。首先,我们需要导入 PyQt5 库,并创建一个主窗口。 ```python from PyQt5.QtWidgets import QApplication, QMainWindow app = QApplication([]) window = QMainWindow() window.show() app.exec_() ``` 接下来,我们需要为主窗口添加菜单栏和工具栏。我们可以使用 `QMenuBar` 和 `QToolBar` 类来实现这一点。 ```python from PyQt5.QtWidgets import QAction, QMenuBar, QToolBar menu_bar = QMenuBar(window) window.setMenuBar(menu_bar) file_menu = menu_bar.addMenu('文件') save_action = QAction('保存', window) file_menu.addAction(save_action) tool_bar = QToolBar(window) window.addToolBar(tool_bar) tool_bar.addAction(save_action) ``` 现在,我们已经添加了菜单栏和工具栏,我们需要添加一些控件来创建保存测试用例的工具。我们可以使用 `QLineEdit`、`QTextEdit` 和 `QPushButton` 等控件来实现这些功能。 ```python from PyQt5.QtWidgets import QLabel, QLineEdit, QTextEdit, QPushButton url_label = QLabel('URL:', window) url_label.move(10, 50) url_edit = QLineEdit(window) url_edit.move(40, 50) url_edit.resize(300, 20) request_label = QLabel('请求:', window) request_label.move(10, 80) request_edit = QTextEdit(window) request_edit.move(10, 100) request_edit.resize(330, 150) save_button = QPushButton('保存', window) save_button.move(10, 270) ``` 最后,我们需要为保存按钮添加一个槽函数,以便在用户单击该按钮时保存测试用例。我们可以使用 `QFileDialog` 类来打开保存文件对话框,并将测试用例保存到用户选择的文件中。 ```python from PyQt5.QtWidgets import QFileDialog def save_test_case(): file_name, _ = QFileDialog.getSaveFileName(window, '保存测试用例', '', 'JSON 文件 (*.json)') if file_name: data = { 'url': url_edit.text(), 'request': request_edit.toPlainText() } # 将 data 写入 file_name 文件中 with open(file_name, 'w') as f: json.dump(data, f) save_button.clicked.connect(save_test_case) ``` 现在,我们已经完成了一个类似于 Postman 的保存测试用例的工具的布局和代码。完整代码如下: ```python import json from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QMenuBar, QToolBar, QLabel, QLineEdit, QTextEdit, QPushButton, QFileDialog app = QApplication([]) window = QMainWindow() window.resize(350, 300) menu_bar = QMenuBar(window) window.setMenuBar(menu_bar) file_menu = menu_bar.addMenu('文件') save_action = QAction('保存', window) file_menu.addAction(save_action) tool_bar = QToolBar(window) window.addToolBar(tool_bar) tool_bar.addAction(save_action) url_label = QLabel('URL:', window) url_label.move(10, 50) url_edit = QLineEdit(window) url_edit.move(40, 50) url_edit.resize(300, 20) request_label = QLabel('请求:', window) request_label.move(10, 80) request_edit = QTextEdit(window) request_edit.move(10, 100) request_edit.resize(330, 150) save_button = QPushButton('保存', window) save_button.move(10, 270) def save_test_case(): file_name, _ = QFileDialog.getSaveFileName(window, '保存测试用例', '', 'JSON 文件 (*.json)') if file_name: data = { 'url': url_edit.text(), 'request': request_edit.toPlainText() } # 将 data 写入 file_name 文件中 with open(file_name, 'w') as f: json.dump(data, f) save_button.clicked.connect(save_test_case) window.show() app.exec_() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值