HTTP请求工具是一个开发小工具-HTTP请求测试工具,仅支持GET、POST请求2中方式, 请求BODY仅支持 JSON字符串。相信很多从事前端开发的人都可能会知道这类软件,它可以帮助我们很好的实现端口调试工作。
软件说明
做网站开发时,经常需要发送请求来测试自己的代码是否OK,这时候模拟发送http请求的工具就起到了很大的作用。
pyqt5.py代码说明
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_())