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>