PyQt5 Designer postman

postman.py

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


<ui version="4.0">
 <class>MainWindowclass>
 <widget class="QMainWindow" name="MainWindow">
  <property name="enabled">
   <bool>truebool>
  property>
  <property name="geometry">
   <rect>
    <x>0x>
    <y>0y>
    <width>653width>
    <height>669height>
   rect>
  property>
  <property name="windowTitle">
   <string>MainWindowstring>
  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>getstring>
            property>
           item>
           <item>
            <property name="text">
             <string>poststring>
            property>
           item>
           <item>
            <property name="text">
             <string>putstring>
            property>
           item>
           <item>
            <property name="text">
             <string>deletestring>
            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>0horstretch>
             <verstretch>0verstretch>
            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::Horizontalenum>
           property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>40width>
             <height>20height>
            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::Horizontalenum>
             property>
             <property name="sizeHint" stdset="0">
              <size>
               <width>40width>
               <height>20height>
              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::Horizontalenum>
             property>
             <property name="sizeHint" stdset="0">
              <size>
               <width>40width>
               <height>20height>
              size>
             property>
            spacer>
           item>
          layout>
         item>
        layout>
       item>
      layout>
     widget>
    item>
   layout>
  widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0x>
     <y>0y>
     <width>653width>
     <height>22height>
    rect>
   property>
  widget>
  <widget class="QStatusBar" name="statusbar"/>
 widget>
 <resources/>
 <connections/>
ui>



你可能感兴趣的:(qt,postman)