Pyqt5制作简易Postman

import requests

from PyQt5.QtWidgets import QApplication, QMessageBox
from PyQt5 import uic

class Postman:

    def __init__(self):
        self.ui = uic.loadUi('Postman.ui')
        self.ui.pushButton.clicked.connect(self.handCalc)
        self.ui.pushButton_2.clicked.connect(self.clear)

    def handCalc(self):
        url = self.ui.lineEdit.text()
        if len(url) == 0:
            message = "请输入有效的url"
        else:
            # 获取请求类型
            request_type = self.ui.comboBox.currentText()
            if request_type == 'POST':
                try:
                    message = requests.post(url = url)   
                    message = message.text()
                except:
                    message = '这不是有效的POST请求'   
            else:
                print('get请求:')
                message = requests.get(url= url)
                message = message.text
        self.ui.plainTextEdit.setPlainText(message)

    def clear(self):
        self.ui.plainTextEdit.setPlainText('')
    

app = QApplication([])
Postman = Postman()
Postman.ui.show()
app.exec_()

Postman.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>612</width>
    <height>427</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Postman</string>
  </property>
  <widget class="QWidget" name="">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>50</y>
     <width>551</width>
     <height>25</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QComboBox" name="comboBox">
      <item>
       <property name="text">
        <string>POST</string>
       </property>
      </item>
      <item>
       <property name="text">
        <string>GET</string>
       </property>
      </item>
     </widget>
    </item>
    <item>
     <widget class="QLineEdit" name="lineEdit"/>
    </item>
    <item>
     <widget class="QPushButton" name="pushButton">
      <property name="text">
       <string>发送</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QWidget" name="">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>100</y>
     <width>551</width>
     <height>271</height>
    </rect>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QPlainTextEdit" name="plainTextEdit">
      <property name="accessibleName">
       <string/>
      </property>
      <property name="plainText">
       <string/>
      </property>
      <property name="placeholderText">
       <string/>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="pushButton_2">
      <property name="autoFillBackground">
       <bool>true</bool>
      </property>
      <property name="text">
       <string>清除</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值