利用PySide2实现简单发包小程序

菜鸡一枚,欢迎大家斧正

目录

前言

一、实现功能

二、实现步骤

1.构造ui文件

2.python代码

总结


前言

本文是对blibli中PySide2教学视频中布置的作业的解答;

视频链接:b站链接

一、实现功能

程序能够利用post和get方式发送请求,可以显示请求头和响应数据;

并且存在url的差错机制,能够进行弹窗提示

最终程序的样式:

 

二、实现步骤

1.构造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>1038</width>
    <height>813</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>简易发包软件</string>
  </property>
  <widget class="QPlainTextEdit" name="Postdata">
   <property name="geometry">
    <rect>
     <x>420</x>
     <y>140</y>
     <width>591</width>
     <height>271</height>
    </rect>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>680</x>
     <y>100</y>
     <width>111</width>
     <height>29</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>13</pointsize>
     <weight>50</weight>
     <bold>false</bold>
    </font>
   </property>
   <property name="text">
    <string>消息体</string>
   </property>
  </widget>
  <widget class="Line" name="line">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>60</y>
     <width>991</width>
     <height>21</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
  <widget class="QPushButton" name="Send">
   <property name="geometry">
    <rect>
     <x>870</x>
     <y>20</y>
     <width>101</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>发送</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="Url">
   <property name="geometry">
    <rect>
     <x>200</x>
     <y>20</y>
     <width>621</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
  <widget class="QComboBox" name="Method">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>20</y>
     <width>121</width>
     <height>31</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>10</pointsize>
    </font>
   </property>
   <property name="currentText">
    <string>GET</string>
   </property>
   <property name="currentIndex">
    <number>0</number>
   </property>
   <item>
    <property name="text">
     <string>GET</string>
    </property>
   </item>
   <item>
    <property name="text">
     <string>POST</string>
    </property>
   </item>
  </widget>
  <widget class="QTableWidget" name="Getdata">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>140</y>
     <width>271</width>
     <height>271</height>
    </rect>
   </property>
   <column>
    <property name="text">
     <string>参数</string>
    </property>
   </column>
   <column>
    <property name="text">
     <string>值</string>
    </property>
   </column>
  </widget>
  <widget class="Line" name="line_2">
   <property name="geometry">
    <rect>
     <x>350</x>
     <y>100</y>
     <width>20</width>
     <height>311</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Vertical</enum>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>100</y>
     <width>76</width>
     <height>33</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>13</pointsize>
    </font>
   </property>
   <property name="text">
    <string>消息头</string>
   </property>
  </widget>
  <widget class="QPushButton" name="Add">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>100</y>
     <width>41</width>
     <height>31</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>17</pointsize>
    </font>
   </property>
   <property name="text">
    <string>+</string>
   </property>
  </widget>
  <widget class="QPushButton" name="Dec">
   <property name="geometry">
    <rect>
     <x>230</x>
     <y>100</y>
     <width>41</width>
     <height>31</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>17</pointsize>
    </font>
   </property>
   <property name="text">
    <string>-</string>
   </property>
  </widget>
  <widget class="QPlainTextEdit" name="printdata">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>450</y>
     <width>971</width>
     <height>301</height>
    </rect>
   </property>
  </widget>
  <widget class="Line" name="line_3">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>420</y>
     <width>961</width>
     <height>16</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
  <widget class="QPushButton" name="Delete">
   <property name="geometry">
    <rect>
     <x>450</x>
     <y>760</y>
     <width>121</width>
     <height>41</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>11</pointsize>
    </font>
   </property>
   <property name="text">
    <string>清除</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_3">
   <property name="geometry">
    <rect>
     <x>890</x>
     <y>780</y>
     <width>141</width>
     <height>21</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>11</pointsize>
    </font>
   </property>
   <property name="text">
    <string>--版权 xxsxss</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

2.python代码

代码如下(示例):

from PySide2.QtWidgets import QApplication, QMessageBox
from PySide2.QtUiTools import QUiLoader
import requests

headers = {
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0'
}

class Stats:

    count = 0
    def __init__(self):
        # 从文件中加载UI定义

        # 从 UI 定义中动态 创建一个相应的窗口对象
        # 注意:里面的控件对象也成为窗口对象的属性了
        # 比如 self.ui.button , self.ui.textEdit
        self.ui = QUiLoader().load('./test.ui')
        self.ui.Send.clicked.connect(self.clicksend)
        self.ui.Add.clicked.connect(self.clickadd)
        self.ui.Dec.clicked.connect(self.clickdec)
        self.ui.Delete.clicked.connect(self.clickdelete)
        self.url = ''
        self.method = ''
        self.getname = ''
        self.getvalue = ''
        self.postvalue = {}
    def geturl(self):
        self.url = self.ui.Url.text()
    def getmethod(self):
        self.method = self.ui.Method.currentText()
    def clicksend(self):
        self.geturl()
        self.getmethod()
        self.Request()
        #print(self.postvalue)
    def clickadd(self):
        self.ui.Getdata.insertRow(self.count)
        self.count = self.count + 1
    def clickdec(self):
        self.ui.Getdata.removeRow(self.count)
        self.count = self.count - 1
    def clickdelete(self):
        self.delete()
    def delete(self):
        self.ui.printdata.clear()
    def getdata(self):
        flag = 0
        for i in range(self.count):
            self.getname = self.ui.Getdata.item(i,0).text()
            self.getvalue = self.ui.Getdata.item(i, 1).text()
            if flag == 0:
                self.url = self.url + '?' + self.getname + '=' + self.getvalue
                flag += 1
            else:
                self.url = self.url + '&' + self.getname + '=' + self.getvalue
    def postdata(self):
        if len(self.ui.Postdata.toPlainText()) != 0:
            data = self.ui.Postdata.toPlainText().split('&')
            for ls in data:
                value = ls.split('=')
                self.postvalue[value[0]] = value[1]
    def Request(self):
        if self.method == "GET":
            self.getdata()
            try:
                response = requests.get(url=self.url, headers=headers)
                self.ui.printdata.insertPlainText(
                    '--------------------------------------------------------------------------------------------------------' + '\n')
                self.ui.printdata.insertPlainText(
                    '请求包' + '\n')
                self.ui.printdata.insertPlainText(
                    '--------------------------------------------------------------------------------------------------------' + '\n')
                self.ui.printdata.insertPlainText('{} {}'.format(self.method, self.url) + '\n')
                for data in response.request.headers.items():
                    self.ui.printdata.insertPlainText(data[0] + ':' + data[1] + '\n')
                self.ui.printdata.insertPlainText(
                    '--------------------------------------------------------------------------------------------------------' + '\n')

                self.ui.printdata.insertPlainText(
                    '\n--------------------------------------------------------------------------------------------------------' + '\n')
                self.ui.printdata.insertPlainText(
                    '响应包' + '\n')
                self.ui.printdata.insertPlainText(
                    '--------------------------------------------------------------------------------------------------------' + '\n')
                self.ui.printdata.insertPlainText('HTTP/1.1 {}'.format(str(response)[11:14]) + '\n')
                for data in response.headers.items():
                    self.ui.printdata.insertPlainText(data[0] + ':' + data[1] + '\n')
                self.ui.printdata.insertPlainText('\n\n{}\n'.format(response.text))
                self.ui.printdata.insertPlainText(
                    '--------------------------------------------------------------------------------------------------------' + '\n')
            except:
                QMessageBox.about(self.ui,'结果',f'''url不存在或请求失败!''')
        elif self.method == "POST":
            self.getdata()
            self.postdata()
            try:
                response = requests.post(url=self.url, headers=headers, data=self.postvalue)
                self.ui.printdata.insertPlainText(
                    '--------------------------------------------------------------------------------------------------------' + '\n')
                self.ui.printdata.insertPlainText(
                    '请求包' + '\n')
                self.ui.printdata.insertPlainText(
                    '--------------------------------------------------------------------------------------------------------' + '\n')
                self.ui.printdata.insertPlainText('{} {}'.format(self.method, self.url) + '\n')
                for data in response.request.headers.items():
                    self.ui.printdata.insertPlainText(data[0] + ':' + data[1] + '\n')
                self.ui.printdata.insertPlainText('\n\n{}\n'.format(self.ui.Postdata.toPlainText()))
                self.ui.printdata.insertPlainText(
                    '\n--------------------------------------------------------------------------------------------------------' + '\n')

                self.ui.printdata.insertPlainText(
                    '--------------------------------------------------------------------------------------------------------' + '\n')
                self.ui.printdata.insertPlainText(
                    '响应包' + '\n')
                self.ui.printdata.insertPlainText(
                    '--------------------------------------------------------------------------------------------------------' + '\n')
                self.ui.printdata.insertPlainText('HTTP/1.1 {}'.format(str(response)[11:14]) + '\n')
                for data in response.headers.items():
                    self.ui.printdata.insertPlainText(data[0] + ':' + data[1] + '\n')
                self.ui.printdata.insertPlainText('\n\n{}\n'.format(response.text))
                self.ui.printdata.insertPlainText(
                    '--------------------------------------------------------------------------------------------------------' + '\n')
            except:
                QMessageBox.about(self.ui, '结果', f'''url不存在或请求失败!''')

app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()

总结

今天这个程序主要用python实现,利用requests来对url解析,得到数据,前端利用pyside2;实现起来较为简单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值