PyQt5 Designer postman

postman.py

python 复制代码
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit, QMessageBox, QComboBox
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile
import requests
import threading


class Req_Thread(threading.Thread):
    def __init__(self, func, args=()):
        super(Req_Thread, self).__init__()
        self.func = func
        self.args = args

    def run(self):
        self.result = self.func(*self.args)

    def get_result(self):
        try:
            return self.result
        except Exception:
            return None


class State():
    def __init__(self):
        self.mothod = 'get'
        qfile_stats = QFile('ui/postman.ui')
        qfile_stats.open(QFile.ReadOnly)
        qfile_stats.close()
        self.ui = QUiLoader().load(qfile_stats)
        # self.window = QMainWindow()
        # self.window.resize(500, 300)
        # self.window.move(300, 200)
        # nume = self.window.menuBar()
        # nume_file = nume.addMenu('文件')
        # nume_file.addAction('新建')
        # nume_file.addAction('打开')
        self.ui.req_method.currentIndexChanged.connect(self.indexChange)
        self.ui.sendhttp.clicked.connect(self.send_tttp_req)
        self.ui.clear_response.clicked.connect(self.clear_response)
        self.ui.add_body.clicked.connect(self.add_body)
        self.ui.remove_body.clicked.connect(self.remove_body)
        self.ui.clear_body.clicked.connect(self.clear_body)
        self.ui.add_header.clicked.connect(self.add_header)
        self.ui.remove_header.clicked.connect(self.remove_header)
        self.ui.clear_header.clicked.connect(self.clear_header)

    def add_header(self):
        rowcount = self.ui.header.rowCount()
        print('add_header')
        self.ui.header.insertRow(rowcount)

    def remove_header(self):
        print('remove_header')
        currentrow = self.ui.header.currentRow()
        self.ui.header.removeRow(currentrow)

    def clear_header(self):
        print('clear_header')
        self.ui.header.clearContents()

    def add_body(self):
        rowcount = self.ui.body.rowCount()
        self.ui.body.insertRow(rowcount)

    def remove_body(self):
        currentrow = self.ui.body.currentRow()
        self.ui.body.removeRow(currentrow)

    def clear_body(self):
        self.ui.body.clearContents()

    def req(self, url, headers, data):
        resp = requests.request(self.mothod, url, headers=headers, data=data)
        return resp

    def send_tttp_req(self):
        self.url = self.ui.httptext.text()
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36"}
        self.data = {}
        if not self.url:
            self.url = 'http://www.baidu.com'
        try:
            rowCount = self.ui.header.rowCount()
            for i in range(rowCount):
                headers_cfg_Name = self.ui.header.item(i, 0).text()
                headers_cfg_Value = self.ui.header.item(i, 1).text()
                self.headers[headers_cfg_Name] = headers_cfg_Value
        except Exception as e:
            print(e)
        try:
            rowCount = self.ui.body.rowCount()
            for i in range(rowCount):
                bodycfgName = self.ui.body.item(i, 0).text()
                bodycfgValue = self.ui.body.item(i, 1).text()
                self.data[bodycfgName] = bodycfgValue
        except Exception as e:
            print(e)
        t = Req_Thread(self.req, args=(self.url, self.headers, self.data))
        t.start()
        t.join()
        resp = t.get_result()
        resp_headers = ''
        for resp_header in resp.headers:
            resp_headers += resp_header + ":" + resp.headers[resp_header] + '\n'
        resp_headers = '发起请求:\n' + resp_headers + '\n\n-------------------------------------------------------\n\n'
        resp = resp_headers + '得到响应:\n' + resp.content.decode("utf-8") + '\n'
        self.ui.response.setPlainText(resp)

    def indexChange(self):
        try:
            if self.ui.req_method.currentIndex() == None or self.ui.req_method.currentIndex() == 0:
                print(self.ui.req_method.currentText())
                self.mothod = self.ui.req_method.currentText()
            elif self.ui.req_method.currentIndex() == 1:
                print(self.ui.req_method.currentText())
                self.mothod = self.ui.req_method.currentText()
            elif self.ui.req_method.currentIndex() == 2:
                print(self.ui.req_method.currentText())
                self.mothod = self.ui.req_method.currentText()
            elif self.ui.req_method.currentIndex() == 3:
                print(self.ui.req_method.currentText())
                self.mothod = self.ui.req_method.currentText()
        except Exception as e:
            raise e

    def clear_response(self):
        self.ui.response.setPlainText('')


app = QApplication([])
state = State()
state.ui.show()
# state.window.show()
app.exec_()

postman.ui

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="enabled">
   <bool>true</bool>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>653</width>
    <height>669</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout_3">
    <item>
     <widget class="QGroupBox" name="groupBox">
      <property name="title">
       <string>请求信息</string>
      </property>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout">
         <item>
          <widget class="QComboBox" name="req_method">
           <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>
          </widget>
         </item>
         <item>
          <widget class="QLineEdit" name="httptext"/>
         </item>
         <item>
          <widget class="QPushButton" name="sendhttp">
           <property name="sizePolicy">
            <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
             <horstretch>0</horstretch>
             <verstretch>0</verstretch>
            </sizepolicy>
           </property>
           <property name="text">
            <string>发送</string>
           </property>
          </widget>
         </item>
        </layout>
       </item>
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout_4">
         <item>
          <widget class="QLabel" name="label_3">
           <property name="text">
            <string>消息头</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="add_header">
           <property name="text">
            <string>+</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="remove_header">
           <property name="text">
            <string>-</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="clear_header">
           <property name="text">
            <string>清空参数</string>
           </property>
          </widget>
         </item>
         <item>
          <spacer name="horizontalSpacer_4">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>40</width>
             <height>20</height>
            </size>
           </property>
          </spacer>
         </item>
         <item>
          <widget class="QLabel" name="label_4">
           <property name="text">
            <string>消息体</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="add_body">
           <property name="text">
            <string>+</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="remove_body">
           <property name="text">
            <string>-</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="clear_body">
           <property name="text">
            <string>清空参数</string>
           </property>
          </widget>
         </item>
        </layout>
       </item>
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout_3">
         <item>
          <widget class="QTableWidget" name="header">
           <column>
            <property name="text">
             <string>键</string>
            </property>
           </column>
           <column>
            <property name="text">
             <string>值</string>
            </property>
           </column>
          </widget>
         </item>
         <item>
          <widget class="QTableWidget" name="body">
           <column>
            <property name="text">
             <string>键</string>
            </property>
           </column>
           <column>
            <property name="text">
             <string>值</string>
            </property>
           </column>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
    </item>
    <item>
     <widget class="QGroupBox" name="groupBox_2">
      <property name="title">
       <string>响应信息</string>
      </property>
      <layout class="QVBoxLayout" name="verticalLayout_4">
       <item>
        <layout class="QVBoxLayout" name="verticalLayout_2">
         <item>
          <widget class="QPlainTextEdit" name="response"/>
         </item>
         <item>
          <layout class="QHBoxLayout" name="horizontalLayout_2">
           <item>
            <spacer name="horizontalSpacer">
             <property name="orientation">
              <enum>Qt::Horizontal</enum>
             </property>
             <property name="sizeHint" stdset="0">
              <size>
               <width>40</width>
               <height>20</height>
              </size>
             </property>
            </spacer>
           </item>
           <item>
            <widget class="QPushButton" name="clear_response">
             <property name="text">
              <string>清空</string>
             </property>
            </widget>
           </item>
           <item>
            <spacer name="horizontalSpacer_2">
             <property name="orientation">
              <enum>Qt::Horizontal</enum>
             </property>
             <property name="sizeHint" stdset="0">
              <size>
               <width>40</width>
               <height>20</height>
              </size>
             </property>
            </spacer>
           </item>
          </layout>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>653</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
相关推荐
苏三有春1 小时前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt
Vanranrr1 小时前
C++ QT
java·c++·qt
兆。2 小时前
掌握 PyQt5:从零开始的桌面应用开发
开发语言·爬虫·python·qt
awonw12 小时前
[java][框架]springMVC(1/2)
测试工具·postman
徒步僧12 小时前
ThingsBoard规则链节点:RPC Call Reply节点详解
qt·microsoft·rpc
可峰科技14 小时前
斗破QT编程入门系列之一:认识Qt:初步使用(四星斗师)
开发语言·qt
我喜欢就喜欢14 小时前
基于qt vs下的视频播放
开发语言·qt·音视频
钱钱钱端14 小时前
【压力测试】如何确定系统最大并发用户数?
自动化测试·软件测试·python·职场和发展·压力测试·postman
CP-DD15 小时前
Qt的架构设计
qt
阿_旭15 小时前
基于YOLO11/v10/v8/v5深度学习的维修工具检测识别系统设计与实现【python源码+Pyqt5界面+数据集+训练代码】
人工智能·python·深度学习·qt·ai