【PyQt5系列二】代理测试窗口---Demo演示

【PyQt5系列二】代理测试窗口---Demo演示,第1张

        在系列上篇讲了下如何部署PyQt5的环境之后,就可以正常使用PyQt5写一写简单的客户端和小的测试工具。接下来会写一个Demo用来测试代理的连通性。

一、需求分析

        做一个测试工具当然是越简洁越好。
        那做一个代理的测试工具无非实现的是这一个功能:输入待测试代理,选择待测试地址/写死待测试地址,开始测试。
        这样一个最基础的框架就搭好了。虽然那么说,在项目实现的过程中肯定会有多多少少的改动,目前就以这个为基础,开始构建客户端。

二、客户端界面构建

        从pycharm中,打开Tools->External Tools->QTdesigner之后,选择Main Window创建即可:

创建好之后,调整一下大小,把我们所需要的组件通过左边的Widget Box中拖拽出来:

         其中输入代理部份用的是Text Edit控件,测试结果展示用的Text Browser控件,分别放入Group Box中,用一个Button用作提交。这样最基础的界面算是搭建好了。

三、功能补全

        将刚才的界面保存为ui文件之后,在pycharm中找到它,右击文件->External Tools->PyUIC之后就会根据你刚才生成的ui文件生成对应的py文件:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'demo.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# 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_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(322, 313)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(240, 240, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(10, 120, 301, 111))
        self.groupBox.setObjectName("groupBox")
        self.textBrowser = QtWidgets.QTextBrowser(self.groupBox)
        self.textBrowser.setGeometry(QtCore.QRect(10, 20, 281, 81))
        self.textBrowser.setObjectName("textBrowser")
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setGeometry(QtCore.QRect(10, 10, 301, 101))
        self.groupBox_2.setObjectName("groupBox_2")
        self.textEdit = QtWidgets.QTextEdit(self.groupBox_2)
        self.textEdit.setGeometry(QtCore.QRect(10, 20, 281, 71))
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 322, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "提交"))
        self.groupBox.setTitle(_translate("MainWindow", "测试结果"))
        self.groupBox_2.setTitle(_translate("MainWindow", "输入待测试代理"))

        下面就是实现功能了。具体分析功能,实现了如下几点:1、获取textEdit组件中输入的内容。2、button触发事件。3、button触发的代理测试请求并获取结果。4、将测试结果返回到textBrowser中进行显示。

        新建方法,先获取输入内容,若为空值d窗提醒:

self.pushButton.clicked.connect(self.getResult)
    def getResult(self):
        proxyInfo = self.textEdit.toPlainText()
        if proxyInfo != "" or len(proxyInfo) != 0:
            print(proxyInfo)
        else:
            QMessageBox.warning(self, "警告", "无测试内容,请按正确格式填写代理!", QMessageBox.Yes)

        再新建一个方法专本用来请求并返回结果:

    def getRes(self,entry):
        proxies = {
            'http': entry,
            'https': entry,
        }
        myipheaders = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 MicroMessenger/6.5.19 NetType/4G Language/zh_TW",
        }
        mainUrl = "https://api.myip.la/en?json"
        headers = myipheaders
        try:
            res = requests.get(url=mainUrl, headers=headers, proxies=proxies, timeout=5)
            status = res.status_code
            reqtime = int(res.elapsed.microseconds / 1000)
        except Exception as e:
            print(e)
            status = 404
            reqtime = "error"
        finally:
            info = f"{entry} 测试状态{status} 测试时间{reqtime}"
            return info

         最后将结果返回显示:

    def getResult(self):
        proxyInfo = self.textEdit.toPlainText()
        if proxyInfo != "" or len(proxyInfo) != 0:
            info = self.getRes(proxyInfo)
            self.textBrowser.append(info)
        else:
            QMessageBox.warning(self, "警告", "无测试内容,请按正确格式填写代理!", QMessageBox.Yes)

 

        这样这个最基本的流程也就走通了。

四、写在最后

        本次使用的代理是IPidea的代理哦,稳定高速新用户白嫖流量,有需求的朋友不妨尝试一下!

        地址:http://www.ipidea.net/?utm-source=csdn&utm-keyword=?wb

        本次实现的是最基础的功能。这里挖个坑,之后会做一个年少回忆,用PyQt5写一个以前用过的小说/漫画下载/阅读工具 !

        最后附上全部代码

        

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'demo.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# 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.
import sys

import requests
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *

class Ui_MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super(Ui_MainWindow, self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(322, 290)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(240, 240, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(10, 120, 301, 111))
        self.groupBox.setObjectName("groupBox")
        self.textBrowser = QtWidgets.QTextBrowser(self.groupBox)
        self.textBrowser.setGeometry(QtCore.QRect(10, 20, 281, 81))
        self.textBrowser.setObjectName("textBrowser")
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setGeometry(QtCore.QRect(10, 10, 301, 101))
        self.groupBox_2.setObjectName("groupBox_2")
        self.textEdit = QtWidgets.QTextEdit(self.groupBox_2)
        self.textEdit.setGeometry(QtCore.QRect(10, 20, 281, 71))
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 322, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        # ```添加button触发```
        # 根据填写信息添加格式化代理
        self.pushButton.clicked.connect(self.getResult)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "提交"))
        self.groupBox.setTitle(_translate("MainWindow", "测试结果"))
        self.groupBox_2.setTitle(_translate("MainWindow", "输入待测试代理"))

    def getResult(self):
        proxyInfo = self.textEdit.toPlainText()
        if proxyInfo != "" or len(proxyInfo) != 0:
            info = self.getRes(proxyInfo)
            self.textBrowser.append(info)
        else:
            QMessageBox.warning(self, "警告", "无测试内容,请按正确格式填写代理!", QMessageBox.Yes)

    def getRes(self,entry):
        proxies = {
            'http': entry,
            'https': entry,
        }
        myipheaders = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 MicroMessenger/6.5.19 NetType/4G Language/zh_TW",
        }
        mainUrl = "https://api.myip.la/en?json"
        headers = myipheaders
        try:
            res = requests.get(url=mainUrl, headers=headers, proxies=proxies, timeout=5)
            status = res.status_code
            reqtime = int(res.elapsed.microseconds / 1000)
        except Exception as e:
            print(e)
            status = 404
            reqtime = "error"
        finally:
            info = f"{entry} 测试状态{status} 测试时间{reqtime}"
            return info

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/884485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-14
下一篇 2022-05-14

发表评论

登录后才能评论

评论列表(0条)

保存