Code
UI设计界面(命名为Ui_http接口测试.py)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'c:\Users\22517\Desktop\python\ui\test.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(794, 516)
self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
self.horizontalLayout.setObjectName("horizontalLayout")
self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
self.plainTextEdit.setObjectName("plainTextEdit")
self.horizontalLayout.addWidget(self.plainTextEdit)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "test-测试-123-abc"))
self.plainTextEdit.setPlaceholderText(_translate("Form", "测试123test"))
self.pushButton.setText(_translate("Form", "测试按钮"))
self.pushButton_2.setText(_translate("Form", "TestButton"))
主程序
import sys
import threading
import json
import datetime
import requests
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QMessageBox, QDialog
from Ui_http接口测试 import Ui_Form # 导入你写的界面类
class MyMainWindow(QDialog, Ui_Form): # 这里也要记得改
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
self.setupUi(self)
self.lineEdit.setPlaceholderText("输入地址https://")
self.pushButton.clicked.connect(self.send)
self.pushButton_2.clicked.connect(self.add)
self.pushButton_3.clicked.connect(self.clear)
self.pushButton_4.clicked.connect(self.remove)
self.plainTextEdit.setPlaceholderText("消息头和消息体不会填勿动")
def add(self):
self.tableWidget.insertRow(self.tableWidget.rowCount())
def remove(self):
try:
self.tableWidget.removeRow(self.tableWidget.currentRow())
except:
self.tableWidget.removeRow(self.tableWidget.rowCount()-1)
def clear(self):
self.plainTextEdit1.clear()
def get_method(self):
self.method = self.comboBox.currentText()
def get_url(self):
self.url = self.lineEdit.text()
def get_headers(self):
self.headers = {}
try:
for row in range(self.tableWidget.rowCount()):
# for col in range(self.tableWidget.columnCount()):
self.headers[self.tableWidget.item(
row, 0).text()] = self.tableWidget.item(row, 1).text()
except AttributeError:
QMessageBox.critical(myWin, "错误", "请删除消息头中空行 ")
def get_body(self):
self.body = self.plainTextEdit.toPlainText()
def insert(self):
self.plainTextEdit1.appendPlainText("----------发送请求----------")
self.plainTextEdit1.appendPlainText(self.start_time)
self.plainTextEdit1.appendPlainText("方法" + " " + self.method)
self.plainTextEdit1.appendPlainText("地址" + " " + self.url)
#self.textEdit.append("Headers"+ " " + str(self.headers))
#self.textEdit.append("Body"+ " " + self.body)
self.plainTextEdit1.appendPlainText("----------得到响应----------")
self.plainTextEdit1.appendPlainText(
"HTTP/1.1 " + str(self.status_code))
self.plainTextEdit1.appendPlainText('Data: ' + self._headers['Date'])
#self.textEdit.append('Server: ' + self._headers['Server'])
self.plainTextEdit1.appendPlainText(
'Content-Type: ' + self._headers['Content-Type'] + '\n')
self.plainTextEdit1.appendPlainText(self._result)
self.plainTextEdit1.appendPlainText("--------------------")
def send(self):
self.get_method()
self.get_url()
self.get_headers()
self.get_body()
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
self.start_time = datetime.datetime.utcnow().strftime(GMT_FORMAT)
try:
func = "requests.request(self.method, self.url, params = params, auth = auth, headers = headers, verfity = verfity, data = self.body)"
if "params" in self.headers:
params = eval(self.headers['params'])
else:
func = func.replace(" params = params,", "")
if "auth" in self.headers:
auth = eval(self.headers['auth'])
else:
func = func.replace(" auth = auth,", "")
if "headers" in self.headers:
headers = eval(self.headers['headers'])
else:
func = func.replace(" headers = headers,", "")
if "verfity" in self.headers:
verfity = eval(self.headers['verfity'])
else:
func = func.replace(" verfity = verfity,", "")
if self.body == "":
func = func.replace(", data = self.body", "")
else:
self.body = json.dumps(eval("{" + self.body + "}"))
response = eval(func)
self.status_code = response.status_code
self._headers = response.headers
self._headers = dict(self._headers)
self._result = response.text
threading.Thread(target=self.insert()).start()
except requests.exceptions.MissingSchema:
QMessageBox.critical(myWin, "错误", "请检查输入内容")
if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainWindow()
myWin.show()
sys.exit(app.exec_())
程序预览
请给我打钱,谢谢!
- 本文链接:http://ayitido.github.io/2022/05/15/HTTP%E6%8E%A5%E5%8F%A3%E6%B5%8B%E8%AF%95/
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。