本帖最后由 GstYon 于 2022-7-4 13:53 编辑
第一次用Pyqt5写的小工具,很简单的的一个小HTTP请求工具。
仅支持GET、POST请求2中方式, 请求BODY仅支持 JSON字符串。
pyqt5.py
[Python] 纯文本查看 复制代码 import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from Ui_pyqt5 import Ui_MainWindow
from PyQt5.QtCore import QCoreApplication
import requests
import json
class MainLogic(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
# 继承父类
super(MainLogic, self).__init__()
# 构造UI
self.setupUi(self)
# 按钮点击事件
def btnClick(self):
url = self.txt_url.toPlainText() # 获取请求URL
request_type = self.request_type.currentText()
request_params = self.request_body.toPlainText() # 获取请求body
request_headers = self.request_header.toPlainText().split("\n") # 获取请求request
params = {}
headers = {
"content-type": "application/x-www-form-urlencoded;",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}
try:
# 请求header
for h in request_headers:
if h =='' or h[0 : h.find(":")] =='' or h[0 : h.find(":")].lower() == 'content-length':
continue
headers[h[0 : h.find(":")]] = h[ h.find(":")+1 :].strip()
if request_params:
params = json.loads(request_params)
if url =='' :
return True
# 判断是否HTTP HTTPS开头
if url[0:7] !='http://' and url[0:8] != 'https://':
url = "http://" + url
if request_type == 'GET':
result = requests.get(url, headers = headers, data = params )
elif request_type =='POST':
result = requests.post(url, headers = headers, json = params )
else:
return True
result.encoding = result.apparent_encoding
# 获取返回内容
outputText = result.text
# 返回格式为JSON时,格式化输出
if result.headers['Content-Type'].find("application/json") > -1:
outputText = json.dumps(result.json(), sort_keys=False, indent=4, separators=(', ', ': ') ,ensure_ascii =False)
# 输出结果到UI
self.response.setPlainText( outputText )
self.respon_code.setText(str(result.status_code))
except Exception as ex:
self.respon_code.setText("")
self.response.setPlainText("请求ERROR:" + str(ex) )
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv )
# QApplication.setQuitOnLastWindowClosed(False)
main = MainLogic()
main.show()
sys.exit(app.exec_())
pyqt5.ui UI页面,自己转UI_pyqt5.py 就可以
[Asm] 纯文本查看 复制代码 <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>925</width>
<height>810</height>
</rect>
</property>
<property name="windowTitle">
<string>HTTP请求工具 By:__墨白n</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QFrame" name="frame_2">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>881</width>
<height>741</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QPlainTextEdit" name="txt_url">
<property name="geometry">
<rect>
<x>140</x>
<y>30</y>
<width>541</width>
<height>70</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777184</height>
</size>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
</font>
</property>
<property name="plainText">
<string/>
</property>
<property name="placeholderText">
<string extracomment="请输入请求地址"/>
</property>
</widget>
<widget class="QPushButton" name="btn_request">
<property name="geometry">
<rect>
<x>710</x>
<y>30</y>
<width>151</width>
<height>73</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>75</width>
<height>73</height>
</size>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
</font>
</property>
<property name="text">
<string>请求</string>
</property>
</widget>
<widget class="QComboBox" name="request_type">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>101</width>
<height>70</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<item>
<property name="text">
<string>GET</string>
</property>
</item>
<item>
<property name="text">
<string>POST</string>
</property>
</item>
</widget>
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>120</y>
<width>841</width>
<height>286</height>
</rect>
</property>
<property name="sizeIncrement">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>请求内容Body</string>
</attribute>
<widget class="QPlainTextEdit" name="request_body">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>836</width>
<height>262</height>
</rect>
</property>
</widget>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>请求头Header</string>
</attribute>
<widget class="QPlainTextEdit" name="request_header">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>841</width>
<height>261</height>
</rect>
</property>
<property name="plainText">
<string/>
</property>
</widget>
</widget>
</widget>
<widget class="QPlainTextEdit" name="response">
<property name="geometry">
<rect>
<x>20</x>
<y>460</y>
<width>841</width>
<height>271</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>20</x>
<y>430</y>
<width>54</width>
<height>12</height>
</rect>
</property>
<property name="text">
<string>返回结果:</string>
</property>
</widget>
<widget class="QLabel" name="respon_code">
<property name="geometry">
<rect>
<x>80</x>
<y>430</y>
<width>54</width>
<height>12</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>925</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections>
<connection>
<sender>btn_request</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>btnClick()</slot>
<hints>
<hint type="sourcelabel">
<x>230</x>
<y>240</y>
</hint>
<hint type="destinationlabel">
<x>462</x>
<y>404</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>btnClick()</slot>
</slots>
</ui>
下载地址:https://wwu.lanzouq.com/iiujB07c3fle |