当前位置: 首页 > article >正文

如何优雅的用PyQt访问http(仅供参考)

使用pydanticdataclaass创建一个数据存储对象

第一种

# coding: utf-8
from typing import Any

import requests
from pydantic import Field, BaseModel


class ResponseModel(BaseModel):
    status: bool = Field(True, description="响应状态")
    message: str = Field('请求成功', description="响应信息")
    error: str = Field('', description="错误信息")
    response: requests.Response = Field(None, description="响应对象")
    result: Any = Field({}, description="响应数据")

    class Config:
        arbitrary_types_allowed = True

线程

# coding: utf-8
from PySide6.QtCore import QThread, Signal, Slot

from common.models import ResponseModel
from loguru import logger


class RequestThread(QThread):
    modelChanged = Signal(ResponseModel)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.request = None

    def run(self):
        if not self.request:
            return
        try:
            result = self.request()
            self.modelChanged.emit(
                ResponseModel(
                    result=result,
                )
            )
        except Exception as e:
            logger.error(e)
            self.modelChanged.emit(
                ResponseModel(
                    status=False,
                    message='请求失败',
                    error=e,
                )
            )

    def setRequest(self, request):
        self.request = request
        if not self.isRunning():
            self.start()

使用函数将请求对象加入至线程中

import requests
from PySide6.QtCore import QCoreApplication

from common.models import ResponseModel
from request_thread import RequestThread


def baidu_request():
    response = requests.get('http://www.baidu.com')
    response.encoding = 'utf-8'
    return response.text


def response_handler(response_model: ResponseModel):
    if response_model.status:
        # TODO: 当请求正确时处理逻辑
        print(response_model.result)
        pass
    else:
        # TODO: 当请求错误时处理逻辑
        print(response_model.message, response_model.error)


if __name__ == '__main__':
    app = QCoreApplication([])
    thread = RequestThread()
    thread.modelChanged.connect(response_handler)
    thread.finished.connect(app.quit)
    thread.setRequest(baidu_request)
    app.exec()


第二种

model模型

# coding: utf-8
from typing import Union, Any

from pydantic import BaseModel, Field


class RequestModel(BaseModel):
    method: str = Field('GET', description='请求方法,如 GET、POST、PUT、DELETE')
    url: str = Field(..., description='请求的 URL 地址')
    params: dict = Field(None, description='请求参数,如 GET 请求时附带的参数')
    data: dict = Field(None, description='请求数据,如 POST 请求时提交的数据')
    json_: dict = Field(None, description='请求数据,如 POST 请求时提交的 json 数据', alias='json')
    headers: dict = Field(None, description='请求头,如 Content-Type、User-Agent 等')
    cookies: dict = Field(None, description='请求 cookies,如登录后获取的 cookie')
    files: dict = Field(None, description='上传的文件,如 POST 请求时上传的文件')
    auth: Union[tuple, list] = Field(None, description='HTTP 认证,如 Basic 认证')
    timeout: int = Field(None, description='请求超时时间,单位为秒')
    allow_redirects: bool = Field(True, description='是否允许重定向')
    proxies: dict = Field(None, description='代理设置')
    hooks: Any = Field(None, description='钩子函数')
    stream: bool = Field(False, description='是否以流的形式响应')
    verify: bool = Field(False, description='是否验证 SSL 证书')
    cert: Union[str, tuple] = Field(None, description='客户端 SSL 证书')

    class Config:
        arbitrary_types_allowed = True

class ResponseModel(BaseModel):
    status: bool = Field(True, description="响应状态")
    message: str = Field('请求成功', description="响应信息")
    error: str = Field('', description="错误信息")
    response: requests.Response = Field(None, description="响应对象")
    result: Any = Field({}, description="响应数据")

    class Config:
        arbitrary_types_allowed = True

请求

# coding: utf-8
import requests
from PyQt5.QtCore import pyqtSignal
from pydantic import BaseModel
from queue import Queue
from ..models import ResponseModel, RequestModel

requests.packages.urllib3.disable_warnings()


class RequestThread(QThread):
    responseChanged = pyqtSignal(BaseModel)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.queue = Queue()

    def run(self):
        while not self.queue.empty():
            method = self.queue.get()  # type: RequestModel

            try:
                data = method.model_dump(mode='python')
                data['json'] = data.pop('json_', None)
                response = requests.request(**data)
                response.raise_for_status()
                self.responseChanged.emit(ResponseModel(response=response))
            except requests.exceptions.RequestException as e:
                self.responseChanged.emit(ResponseModel(status=False, message='请求失败',error=str(e)))

    def setRequest(self, method: RequestModel):
        self.queue.put(method)
        if not self.isRunning():
            self.start()

运用

from PyQt5.QtWidgets import QApplication

from common import RequestThread, RequestModel, ResponseModel


def response_handler(response_model: ResponseModel):
    if response_model.status:
        # TODO: 当请求正确时处理逻辑
        print(response_model.response.text)
        pass
    else:
        # TODO: 当请求错误时处理逻辑
        print(response_model.message, response_model.error)


# Create a QApplication instance
app = QApplication([])

# Create a request thread and start it
request_thread = RequestThread()
request_thread.responseChanged.connect(response_handler)
request_thread.setRequest(RequestModel(url='http://www.baidu.com'))
request_thread.finished.connect(app.quit)
app.exec_()

http://www.kler.cn/a/414316.html

相关文章:

  • (vue)启动项目报错The project seems to require pnpm but it‘s not installed
  • 环境崩溃后的重新建立
  • [UUCTF 2022 新生赛]ez_rce
  • Linux文件基础
  • Paddle Inference部署推理(八)
  • 机器学习模型——线性回归
  • 架构-微服务-服务治理
  • Wrapper包装类
  • 关于在大模型智能体中知识图谱构建与指令应用
  • Go语言中的sync.Pool详解:高效对象复用
  • Kafka面试题(五)--内含面试重点
  • 深度学习:代码预训练模型
  • Golang项目:实现生产者消费者模式
  • uniapp 安卓和ios震动方法,支持息屏和后台震动,ios和安卓均通过测试
  • 数据结构与算法(排序算法)
  • STM32-C语言基础知识
  • TheadLocal出现的内存泄漏具体泄漏的是什么?弱引用在里面有什么作用?什么情景什么问题?
  • 水母形状电池:助力机器人性能提升
  • 【西瓜书】决策树
  • 网络空间安全之一个WH的超前沿全栈技术深入学习之路(13-2)白帽必经之路——如何用Metasploit 渗透到她的心才不会让我释怀
  • 力扣刷题--42.接雨水【图文详解|超级详细】
  • JAVA项目-------医院挂号系统
  • 鸿蒙征文|鸿蒙技术分享:使用到的开发框架和技术概览
  • 一些k8s和docker的命令
  • javaweb-day03-前端零碎
  • golang 实现比特币内核:如何接入 RPC 后端获得特定交易的二进制数据