当前位置: 首页 > article >正文

[白月黑羽]关于仿写类postman功能软件题目的解答

原题:
在这里插入图片描述
答:
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()

class TableWidgetFilter(QObject):
    def __init__(self, parent=None):
        super(TableWidgetFilter, self).__init__(parent)
    
    def eventFilter(self, watched, event):
        if event.type() == QEvent.MouseButtonRelease:
            idx = watched.parentWidget().indexAt(event.position().toPoint())
            if(idx.row() < 0):
                watched.parentWidget().setCurrentItem(None)
        return super(TableWidgetFilter, self).eventFilter( watched, event)

# 用于打印请求消息, 参数为 PreparedRequest 对象
def pretty_print_request(req):
    if req.body == None:
        msgBody = ''
    else:
        msgBody = req.body
    
    # 打印请求消息,如果为空不拼接进去
    result = '\n----------- 发送请求 -----------'
    if req.method!= None and req.url!= None:
        result = result + '\n' + req.method + ' ' + req.url 
    if req.headers != None and len(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

# 用于打印响应消息
def pretty_print_response(res) -> str:
    result = '\n----------- 得到响应 -----------'
    if res.status_code!= None:
        result = result + str('\nHTTP/1.1 '+ str(res.status_code))
    if res.headers!= None and len(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


class MainWindow:
    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)

    def add_header(self):
        self.ui.headersTable.insertRow(self.ui.headersTable.rowCount())
    
    def remove_header(self):
        row = self.ui.headersTable.currentRow()
        if row >= 0:
            self.ui.headersTable.removeRow(row)
            self.ui.headersTable.setCurrentItem(None)
    
    def send(self):
        #获取多个数据源
        request_type = self.ui.requestBox.currentText()
        request_url = self.ui.urlEdit.text()
        request_headers = {}
        for i in range(self.ui.headersTable.rowCount()):
            if self.ui.headersTable.item(i, 0) == None or 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))
            return

    def clear_result(self):
        self.ui.resultText.clear()


app = QApplication([])
mainWindow = MainWindow()
mainWindow.ui.show()
app.exec()

动态加载的main.ui文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>534</width>
    <height>519</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>HTTP接口测试</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_3">
   <item>
    <layout class="QVBoxLayout" name="verticalLayout_2">
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QComboBox" name="requestBox">
         <item>
          <property name="text">
           <string>GET</string>
          </property>
         </item>
         <item>
          <property name="text">
           <string>POST</string>
          </property>
         </item>
        </widget>
       </item>
       <item>
        <widget class="QLineEdit" name="urlEdit"/>
       </item>
       <item>
        <widget class="QPushButton" name="sendBtn">
         <property name="text">
          <string>发送</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <widget class="Line" name="line">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QSplitter" name="splitter_3">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <widget class="QSplitter" name="splitter_2">
        <property name="orientation">
         <enum>Qt::Vertical</enum>
        </property>
        <widget class="QSplitter" name="splitter">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <widget class="QLabel" name="label">
          <property name="font">
           <font>
            <pointsize>12</pointsize>
           </font>
          </property>
          <property name="layoutDirection">
           <enum>Qt::LeftToRight</enum>
          </property>
          <property name="text">
           <string>消息头</string>
          </property>
          <property name="alignment">
           <set>Qt::AlignCenter</set>
          </property>
         </widget>
         <widget class="QPushButton" name="addBtn">
          <property name="sizePolicy">
           <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
          <property name="maximumSize">
           <size>
            <width>50</width>
            <height>16777215</height>
           </size>
          </property>
          <property name="text">
           <string>+</string>
          </property>
         </widget>
         <widget class="QPushButton" name="removeBtn">
          <property name="sizePolicy">
           <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
          <property name="maximumSize">
           <size>
            <width>50</width>
            <height>16777215</height>
           </size>
          </property>
          <property name="text">
           <string>-</string>
          </property>
         </widget>
        </widget>
        <widget class="QTableWidget" name="headersTable">
         <column>
          <property name="text">
           <string>名称</string>
          </property>
         </column>
         <column>
          <property name="text">
           <string></string>
          </property>
         </column>
        </widget>
       </widget>
       <widget class="Line" name="line_2">
        <property name="minimumSize">
         <size>
          <width>15</width>
          <height>0</height>
         </size>
        </property>
        <property name="orientation">
         <enum>Qt::Vertical</enum>
        </property>
       </widget>
       <widget class="QWidget" name="layoutWidget_2">
        <layout class="QVBoxLayout" name="verticalLayout">
         <item>
          <widget class="QLabel" name="label_2">
           <property name="font">
            <font>
             <pointsize>12</pointsize>
            </font>
           </property>
           <property name="text">
            <string>消息体</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPlainTextEdit" name="bodyEdit">
           <property name="enabled">
            <bool>true</bool>
           </property>
          </widget>
         </item>
        </layout>
       </widget>
      </widget>
     </item>
     <item>
      <widget class="Line" name="line_3">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QPlainTextEdit" name="resultText">
       <property name="readOnly">
        <bool>true</bool>
       </property>
      </widget>
     </item>
     <item alignment="Qt::AlignHCenter">
      <widget class="QPushButton" name="clearBtn">
       <property name="maximumSize">
        <size>
         <width>75</width>
         <height>16777215</height>
        </size>
       </property>
       <property name="text">
        <string>清除</string>
       </property>
      </widget>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

使用效果:
请添加图片描述


http://www.kler.cn/a/396492.html

相关文章:

  • MySQL【五】
  • 解决背景图因为图片路径中携带括号导致图片无法显示的问题
  • DB-GPT系列(四):DB-GPT六大基础应用场景part1
  • mysql每日一题(上升的温度,date数据的计算)
  • 自动驾驶系列—从数据采集到存储:解密自动驾驶传感器数据采集盒子的关键技术
  • 【C#设计模式(4)——构建者模式(Builder Pattern)】
  • Leetcode 每日一题 125.验证回文串
  • PLC如何支持GEM300标准?SECS/GEM通讯协议
  • 卷积层(CNN)全面解析
  • 如何使用 python 中的 Pillow 创建可自定义的图标生成器
  • 软件工程视角:Git 基础与实践
  • NLP开发常见问题
  • 微信小程序之路由跳转传数据及接收
  • JWTUtil工具类
  • 加深深度学习矩阵计算理解--用人类直觉 走进线性代数(非应试)
  • 自存 关于RestController请求传参数 前端和后端相关
  • web——upload-labs——第五关——大小写绕过绕过
  • HarmonyOS本地存储-Preferences(用户首选项)的使用
  • MATLAB 使用教程 —— 常用函数
  • Git 时想要放弃当前的 commit 操作
  • Javaweb-day11案例(文件)
  • 基于stm32的智能变频电冰箱系统
  • 网络安全练习之 ctfshow_web
  • 力扣 LeetCode 232. 用栈实现队列(Day5:栈与队列)
  • STM32F4 RTC实时时钟STM32 Cube实例
  • 掌握ECMAScript模块化:构建高效JavaScript应用