原题:
答:
python文件如下
from PySide6.QtWidgets import QApplication, QMessageBox,QTableWidgetItem,QHeaderView,QWidget,QTableWidget
from PySide6.QtCore import QEvent,QObject
from PySide6.QtUiTools import QUiLoader
import time
import requests
uiLoader = QUiLoader()classTableWidgetFilter(QObject):def__init__(self, parent=None):super(TableWidgetFilter, self).__init__(parent)defeventFilter(self, watched, event):if event.type()== QEvent.MouseButtonRelease:
idx = watched.parentWidget().indexAt(event.position().toPoint())if(idx.row()<0):
watched.parentWidget().setCurrentItem(None)returnsuper(TableWidgetFilter, self).eventFilter( watched, event)# 用于打印请求消息, 参数为 PreparedRequest 对象defpretty_print_request(req):if req.body ==None:
msgBody =''else:
msgBody = req.body
# 打印请求消息,如果为空不拼接进去
result ='\n----------- 发送请求 -----------'if req.method!=Noneand req.url!=None:
result = result +'\n'+ req.method +' '+ req.url
if req.headers !=Noneandlen(req.headers)>0:
result = result +'\n'
result = result +str('\n'.join('{}: {}'.format(k, v)for k, v in req.headers.items()))if msgBody!='':
result = result +str('\n'+ msgBody)return result
# 用于打印响应消息defpretty_print_response(res)->str:
result ='\n----------- 得到响应 -----------'if res.status_code!=None:
result = result +str('\nHTTP/1.1 '+str(res.status_code))if res.headers!=Noneandlen(res.headers)>0:
result = result +'\n'
result = result +str('\n'.join('{}: {}'.format(k, v)for k, v in res.headers.items()))if res.text!='':
result = result +str('\n'+ res.text)return result
classMainWindow:def__init__(self):
self.ui = uiLoader.load('.\\main.ui')
self.ui.addBtn.clicked.connect(self.add_header)
self.ui.removeBtn.clicked.connect(self.remove_header)
self.ui.headersTable.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)# 创建事件过滤器实例
self.event_filter = TableWidgetFilter(self.ui.headersTable)
self.ui.headersTable.viewport().installEventFilter(self.event_filter)
self.ui.clearBtn.clicked.connect(self.clear_result)
self.ui.sendBtn.clicked.connect(self.send)defadd_header(self):
self.ui.headersTable.insertRow(self.ui.headersTable.rowCount())defremove_header(self):
row = self.ui.headersTable.currentRow()if row >=0:
self.ui.headersTable.removeRow(row)
self.ui.headersTable.setCurrentItem(None)defsend(self):#获取多个数据源
request_type = self.ui.requestBox.currentText()
request_url = self.ui.urlEdit.text()
request_headers ={}for i inrange(self.ui.headersTable.rowCount()):if self.ui.headersTable.item(i,0)==Noneor self.ui.headersTable.item(i,1)==None:continue
key = self.ui.headersTable.item(i,0).text()
value = self.ui.headersTable.item(i,1).text()
request_headers[key]= value
request_body = self.ui.bodyEdit.toPlainText()try:
req = requests.Request(request_type,
request_url,
headers=request_headers,
data=request_body)
prepared = req.prepare()except requests.exceptions.RequestException as e:
QMessageBox.warning(self.ui,"错误","请求参数错误\n"+str(e))return
self.ui.resultText.appendPlainText(pretty_print_request(prepared))#发送包try:
res = requests.Session().send(prepared)
self.ui.resultText.appendPlainText(pretty_print_response(res))except requests.exceptions.RequestException as e:
QMessageBox.warning(self.ui,"错误","请求失败\n"+str(e))returndefclear_result(self):
self.ui.resultText.clear()
app = QApplication([])
mainWindow = MainWindow()
mainWindow.ui.show()
app.exec()
动态加载的main.ui文件
<?xml version="1.0" encoding="UTF-8"?><uiversion="4.0"><class>Form</class><widgetclass="QWidget"name="Form"><propertyname="geometry"><rect><x>0</x><y>0</y><width>534</width><height>519</height></rect></property><propertyname="windowTitle"><string>HTTP接口测试</string></property><layoutclass="QVBoxLayout"name="verticalLayout_3"><item><layoutclass="QVBoxLayout"name="verticalLayout_2"><item><layoutclass="QHBoxLayout"name="horizontalLayout"><item><widgetclass="QComboBox"name="requestBox"><item><propertyname="text"><string>GET</string></property></item><item><propertyname="text"><string>POST</string></property></item></widget></item><item><widgetclass="QLineEdit"name="urlEdit"/></item><item><widgetclass="QPushButton"name="sendBtn"><propertyname="text"><string>发送</string></property></widget></item></layout></item><item><widgetclass="Line"name="line"><propertyname="orientation"><enum>Qt::Horizontal</enum></property></widget></item><item><widgetclass="QSplitter"name="splitter_3"><propertyname="orientation"><enum>Qt::Horizontal</enum></property><widgetclass="QSplitter"name="splitter_2"><propertyname="orientation"><enum>Qt::Vertical</enum></property><widgetclass="QSplitter"name="splitter"><propertyname="orientation"><enum>Qt::Horizontal</enum></property><widgetclass="QLabel"name="label"><propertyname="font"><font><pointsize>12</pointsize></font></property><propertyname="layoutDirection"><enum>Qt::LeftToRight</enum></property><propertyname="text"><string>消息头</string></property><propertyname="alignment"><set>Qt::AlignCenter</set></property></widget><widgetclass="QPushButton"name="addBtn"><propertyname="sizePolicy"><sizepolicyhsizetype="Fixed"vsizetype="Fixed"><horstretch>0</horstretch><verstretch>0</verstretch></sizepolicy></property><propertyname="maximumSize"><size><width>50</width><height>16777215</height></size></property><propertyname="text"><string>+</string></property></widget><widgetclass="QPushButton"name="removeBtn"><propertyname="sizePolicy"><sizepolicyhsizetype="Fixed"vsizetype="Fixed"><horstretch>0</horstretch><verstretch>0</verstretch></sizepolicy></property><propertyname="maximumSize"><size><width>50</width><height>16777215</height></size></property><propertyname="text"><string>-</string></property></widget></widget><widgetclass="QTableWidget"name="headersTable"><column><propertyname="text"><string>名称</string></property></column><column><propertyname="text"><string>值</string></property></column></widget></widget><widgetclass="Line"name="line_2"><propertyname="minimumSize"><size><width>15</width><height>0</height></size></property><propertyname="orientation"><enum>Qt::Vertical</enum></property></widget><widgetclass="QWidget"name="layoutWidget_2"><layoutclass="QVBoxLayout"name="verticalLayout"><item><widgetclass="QLabel"name="label_2"><propertyname="font"><font><pointsize>12</pointsize></font></property><propertyname="text"><string>消息体</string></property><propertyname="alignment"><set>Qt::AlignCenter</set></property></widget></item><item><widgetclass="QPlainTextEdit"name="bodyEdit"><propertyname="enabled"><bool>true</bool></property></widget></item></layout></widget></widget></item><item><widgetclass="Line"name="line_3"><propertyname="orientation"><enum>Qt::Horizontal</enum></property></widget></item><item><widgetclass="QPlainTextEdit"name="resultText"><propertyname="readOnly"><bool>true</bool></property></widget></item><itemalignment="Qt::AlignHCenter"><widgetclass="QPushButton"name="clearBtn"><propertyname="maximumSize"><size><width>75</width><height>16777215</height></size></property><propertyname="text"><string>清除</string></property></widget></item></layout></item></layout></widget><resources/><connections/></ui>
使用效果:
本文转载自: https://blog.csdn.net/m0_37662818/article/details/143785894
版权归原作者 m0_37662818 所有, 如有侵权,请联系我们删除。
版权归原作者 m0_37662818 所有, 如有侵权,请联系我们删除。