import sys
import requests.exceptions
from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox, QInputDialog,QTableWidgetItem
from http_ui import Ui_formWidget # 导入通过 pyside6-uic 生成的 UI 类
class Stats(QMainWindow): # 让 Stats 继承自 QMainWindow
def __init__(self):
super().__init__() # 调用父类的构造函数
self.setWindowTitle("HTTP接口测试工具")
# 使用生成的 UI 类
self.ui = Ui_formWidget()
# 初始化界面
self.ui.setupUi(self) # 将当前 QMainWindow(Stats)传递给 UI 类
# 连接发送按钮的点击事件
self.ui.sendButton.clicked.connect(self.send_request)
# 点击➕按钮添加请求头
self.ui.addHeaderButton.clicked.connect(self.add_header)
self.ui.deleteHeaderButton.clicked.connect(self.delete_header)
# 连接清除按钮的点击事件
self.ui.clearButton.clicked.connect(self.clear_response)
# 请求头的存储
self.headers = []
def add_header(self):
# 获取用户输入的请求头的名称和值
header_name,ok_name = QInputDialog.getText(self,"请求头名称", "请输入请求头的名称:")
if not ok_name or not header_name:
return # 如果没有输入名称,则返回
header_value, ok_value = QInputDialog.getText(self, "请求头值", "请输入请求头的值:")
if not ok_value or not header_value:
return # 如果没有输入值,则返回
if header_name and header_value:
self.headers.append((header_name, header_value)) # 添加请求头到列表
self.update_headers_table() # 更新表格显示
def delete_header(self):
# 删除选中的请求头
selected_row = self.ui.headersWidget.currentRow()
if selected_row >= 0:
del self.headers[selected_row]
self.update_headers_table()
def update_headers_table(self):
# 更新表格内容
self.ui.headersWidget.setRowCount(len(self.headers)) # 设置表格的行数
for i, (header_name, header_value) in enumerate(self.headers):
self.ui.headersWidget.setItem(i, 0, QTableWidgetItem(header_name)) # 请求头名称
self.ui.headersWidget.setItem(i, 1, QTableWidgetItem(header_value)) # 请求头值
def send_request(self):
# 获取用户输入的URL和请求类型
url = self.ui.urlEdit.text()
method = self.ui.comboBox.currentText()
# 将请求头从列表转换为字典
headers = {name: value for name, value in self.headers}
# 请求体
body = self.ui.requestBodyWidget.toPlainText()
try:
if method == "GET":
response = requests.get(url, headers=headers)
elif method == "POST":
response = requests.post(url, headers=headers, data=body)
elif method == "PUT":
response = requests.put(url, headers=headers, data=body)
elif method == "DELETE":
response = requests.delete(url, headers=headers, data=body)
elif method == "PATCH":
response = requests.patch(url, headers=headers, data=body)
else:
raise ValueError("不支持的请求方法")
# 显示响应结果
self.ui.responseWidget.setPlainText(response.text) # 假设有一个显示响应的文本框
except requests.exceptions.RequestException as e:
QMessageBox.critical(self, "请求失败", f"发生错误: {str(e)}")
def clear_response(self):
# 清除响应显示区域
self.ui.responseWidget.clear()
self.ui.headersWidget.clearContents()
if __name__ == '__main__':
app = QApplication(sys.argv)
stats = Stats() # 创建 Stats 对象(现在它是 QMainWindow 的子类)
stats.show() # 显示窗口
app.exec()
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'http.ui'
##
## Created by: Qt User Interface Compiler version 6.8.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QComboBox, QFrame, QHeaderView,
QLabel, QLineEdit, QPlainTextEdit, QPushButton,
QSizePolicy, QTableWidget, QTableWidgetItem, QTextEdit,
QToolButton, QWidget)
class Ui_formWidget(object):
def setupUi(self, formWidget):
if not formWidget.objectName():
formWidget.setObjectName(u"formWidget")
formWidget.resize(869, 732)
self.comboBox = QComboBox(formWidget)
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.setObjectName(u"comboBox")
self.comboBox.setGeometry(QRect(50, 30, 91, 31))
self.sendButton = QPushButton(formWidget)
self.sendButton.setObjectName(u"sendButton")
self.sendButton.setGeometry(QRect(750, 30, 91, 31))
self.line = QFrame(formWidget)
self.line.setObjectName(u"line")
self.line.setGeometry(QRect(10, 70, 861, 20))
self.line.setFrameShape(QFrame.Shape.HLine)
self.line.setFrameShadow(QFrame.Shadow.Sunken)
self.label = QLabel(formWidget)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(20, 110, 54, 16))
font = QFont()
font.setPointSize(12)
self.label.setFont(font)
self.addHeaderButton = QToolButton(formWidget)
self.addHeaderButton.setObjectName(u"addHeaderButton")
self.addHeaderButton.setGeometry(QRect(80, 100, 41, 31))
self.deleteHeaderButton = QToolButton(formWidget)
self.deleteHeaderButton.setObjectName(u"deleteHeaderButton")
self.deleteHeaderButton.setGeometry(QRect(130, 100, 41, 31))
self.line_2 = QFrame(formWidget)
self.line_2.setObjectName(u"line_2")
self.line_2.setGeometry(QRect(400, 80, 16, 291))
self.line_2.setFrameShape(QFrame.Shape.VLine)
self.line_2.setFrameShadow(QFrame.Shadow.Sunken)
self.line_3 = QFrame(formWidget)
self.line_3.setObjectName(u"line_3")
self.line_3.setGeometry(QRect(0, 360, 871, 20))
self.line_3.setFrameShape(QFrame.Shape.HLine)
self.line_3.setFrameShadow(QFrame.Shadow.Sunken)
self.urlEdit = QLineEdit(formWidget)
self.urlEdit.setObjectName(u"urlEdit")
self.urlEdit.setGeometry(QRect(150, 30, 581, 31))
self.headersWidget = QTableWidget(formWidget)
if (self.headersWidget.columnCount() < 2):
self.headersWidget.setColumnCount(2)
__qtablewidgetitem = QTableWidgetItem()
self.headersWidget.setHorizontalHeaderItem(0, __qtablewidgetitem)
__qtablewidgetitem1 = QTableWidgetItem()
self.headersWidget.setHorizontalHeaderItem(1, __qtablewidgetitem1)
self.headersWidget.setObjectName(u"headersWidget")
self.headersWidget.setGeometry(QRect(20, 140, 381, 221))
self.requestBodyWidget = QPlainTextEdit(formWidget)
self.requestBodyWidget.setObjectName(u"requestBodyWidget")
self.requestBodyWidget.setGeometry(QRect(420, 130, 431, 221))
self.label_2 = QLabel(formWidget)
self.label_2.setObjectName(u"label_2")
self.label_2.setGeometry(QRect(610, 100, 54, 16))
self.label_2.setFont(font)
self.responseWidget = QTextEdit(formWidget)
self.responseWidget.setObjectName(u"responseWidget")
self.responseWidget.setGeometry(QRect(20, 390, 831, 291))
self.clearButton = QPushButton(formWidget)
self.clearButton.setObjectName(u"clearButton")
self.clearButton.setGeometry(QRect(390, 690, 71, 31))
self.retranslateUi(formWidget)
QMetaObject.connectSlotsByName(formWidget)
# setupUi
def retranslateUi(self, formWidget):
formWidget.setWindowTitle(QCoreApplication.translate("formWidget", u"HTTP\u63a5\u53e3\u6d4b\u8bd5", None))
self.comboBox.setItemText(0, QCoreApplication.translate("formWidget", u"GET", None))
self.comboBox.setItemText(1, QCoreApplication.translate("formWidget", u"POST", None))
self.comboBox.setItemText(2, QCoreApplication.translate("formWidget", u"PUT", None))
self.comboBox.setItemText(3, QCoreApplication.translate("formWidget", u"DELETE", None))
self.comboBox.setItemText(4, QCoreApplication.translate("formWidget", u"PATCH", None))
self.sendButton.setText(QCoreApplication.translate("formWidget", u"\u53d1\u9001", None))
self.label.setText(QCoreApplication.translate("formWidget", u"\u6d88\u606f\u5934", None))
self.addHeaderButton.setText(QCoreApplication.translate("formWidget", u"+", None))
self.deleteHeaderButton.setText(QCoreApplication.translate("formWidget", u"-", None))
___qtablewidgetitem = self.headersWidget.horizontalHeaderItem(0)
___qtablewidgetitem.setText(QCoreApplication.translate("formWidget", u"\u540d\u79f0", None));
___qtablewidgetitem1 = self.headersWidget.horizontalHeaderItem(1)
___qtablewidgetitem1.setText(QCoreApplication.translate("formWidget", u"\u503c", None));
self.label_2.setText(QCoreApplication.translate("formWidget", u"\u6d88\u606f\u4f53", None))
self.clearButton.setText(QCoreApplication.translate("formWidget", u"\u6e05\u9664", None))
# retranslateUi
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>formWidget</class>
<widget class="QWidget" name="formWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>869</width>
<height>732</height>
</rect>
</property>
<property name="windowTitle">
<string>HTTP接口测试</string>
</property>
<widget class="QComboBox" name="comboBox">
<property name="geometry">
<rect>
<x>50</x>
<y>30</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<item>
<property name="text">
<string>GET</string>
</property>
</item>
<item>
<property name="text">
<string>POST</string>
</property>
</item>
<item>
<property name="text">
<string>PUT</string>
</property>
</item>
<item>
<property name="text">
<string>DELETE</string>
</property>
</item>
<item>
<property name="text">
<string>PATCH</string>
</property>
</item>
</widget>
<widget class="QPushButton" name="sendButton">
<property name="geometry">
<rect>
<x>750</x>
<y>30</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>发送</string>
</property>
</widget>
<widget class="Line" name="line">
<property name="geometry">
<rect>
<x>10</x>
<y>70</y>
<width>861</width>
<height>20</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>20</x>
<y>110</y>
<width>54</width>
<height>16</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>消息头</string>
</property>
</widget>
<widget class="QToolButton" name="addHeaderButton">
<property name="geometry">
<rect>
<x>80</x>
<y>100</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>+</string>
</property>
</widget>
<widget class="QToolButton" name="deleteHeaderButton">
<property name="geometry">
<rect>
<x>130</x>
<y>100</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>-</string>
</property>
</widget>
<widget class="Line" name="line_2">
<property name="geometry">
<rect>
<x>400</x>
<y>80</y>
<width>16</width>
<height>291</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Orientation::Vertical</enum>
</property>
</widget>
<widget class="Line" name="line_3">
<property name="geometry">
<rect>
<x>0</x>
<y>360</y>
<width>871</width>
<height>20</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
</widget>
<widget class="QLineEdit" name="urlEdit">
<property name="geometry">
<rect>
<x>150</x>
<y>30</y>
<width>581</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QTableWidget" name="headersWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>140</y>
<width>381</width>
<height>221</height>
</rect>
</property>
<column>
<property name="text">
<string>名称</string>
</property>
</column>
<column>
<property name="text">
<string>值</string>
</property>
</column>
</widget>
<widget class="QPlainTextEdit" name="requestBodyWidget">
<property name="geometry">
<rect>
<x>420</x>
<y>130</y>
<width>431</width>
<height>221</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>610</x>
<y>100</y>
<width>54</width>
<height>16</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>消息体</string>
</property>
</widget>
<widget class="QTextEdit" name="responseWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>390</y>
<width>831</width>
<height>291</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="clearButton">
<property name="geometry">
<rect>
<x>390</x>
<y>690</y>
<width>71</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>清除</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>