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>
相关推荐
lw向北.6 小时前
Qt For Android之环境搭建(Qt 5.12.11 Qt下载SDK的处理方案)
android·开发语言·qt
小灰灰搞电子6 小时前
Qt实现Android的图案密码(图形解锁)源码分享
开发语言·qt
测试者家园9 小时前
ChatGPT与Postman协作完成接口测试(一)
软件测试·chatgpt·接口测试·postman·用chatgpt做软件测试·测试图书·质量效能
<e^πi+1=0>10 小时前
Postbot使用教程
postman
kiiila17 小时前
【Qt】对象树(生命周期管理)和字符集(cout打印乱码问题)
开发语言·qt
黄金右肾20 小时前
Qt之数据库使用(十四)
sql·qt·sqlite·database
杨德杰1 天前
QT多媒体开发(一):概述
qt·音视频·多媒体
小王爱吃月亮糖1 天前
QT开发【常用控件1】-Layouts & Spacers
开发语言·前端·c++·qt·visual studio
864记忆1 天前
关于opencv、Qt、msvc编译器之间的关系
人工智能·qt·opencv
炭烤玛卡巴卡1 天前
学习postman工具使用
学习·测试工具·postman