python 做的request模块封装工具类

本文记录了一个使用Python request模块封装的工具类,旨在方便日后调用。内容包括封装的动机和基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.python 做的request模块封装工具类

记录下,避免以后用到。


import time
from requests.adapters import HTTPAdapter
import json
import requests
from requests import exceptions

class HttpRequests(object):
     """
     eg: request = HttpRequests()
         response = request(method, url, data)
         or
         response = request.send_request(method, url, data)
         print(response.text)
    """
     def __init__(self):
        self.session = requests.Session()
        self.session.mount('http://', HTTPAdapter(max_retries=3))
        self.session.mount('https://', HTTPAdapter(max_retries=3))
        # exceptions.Timeout

     def send_request(self, method, url, data=None, params_type='JSON',  **kwargs):
         """
         request 请求封装 默认使用json格式
         """
         method = method.upper()
         params_type = params_type
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值