Python requests请求通用类

Python requests请求通用类


import requests

"""请求通用类"""

class HttpRequest(object):
    def __init__(self):
        super(HttpRequest, self).__init__()

    """请求通用方法"""

    @staticmethod
    def logging_hook(response):
        logging.info(f"response url:{response.url}")
        logging.info(f"response state:{response.status_code} {response.reason}")
        logging.info(f"response encoding:{response.encoding}")
        logging.info(f"response Content-Type:{response.headers.get('Content-Type')}")
        logging.info(f"response text:{response.text}")

    @staticmethod
    def http_request(
            url,
            method="post",
            proxie=None,
            param=None,
            data=None,
            content_type="application/json",
            header=None,
            cookie=None,
            timeout=(3, 10),
            verify=False):

        for _ in range(2):
            try:
                with requests.session() as session:
                    session.hooks["response"] = [HttpRequest.logging_hook]
                    session.verify = False if not verify else True
                    if isinstance(proxie, dict) and proxie.get("http") and proxie.get("https"):
                        session.proxies = proxie
                    logging.info(f"http_request:url:{url} method:{method} param:{param} data:{data}")
                    if method == "get":
                        response = session.get(url=url, params=param, headers=header, cookies=cookie, timeout=timeout)
                    elif method == "post":
                        if content_type and content_type == "application/json":
                            response = session.post(url=url, json=data, headers=header, cookies=cookie, timeout=timeout)
                        else:
                            response = session.post(url=url, data=data, headers=header, cookies=cookie, timeout=timeout)
                    else:
                        raise Exception("Request method not supported!")

                    logging.info(f"http_response:status_code:{response.status_code} reason:{response.reason} content:{response.content}")

                    return response.status_code, response.text
            except BaseException:
                logging.error(f"http_request error:{traceback.format_exc()}")

        return None, {}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值