Python 网络请求工具库

retry 见链接 :https://blog.csdn.net/CXY00000/article/details/132482838

# !/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: [email protected]
@file: util_network.py
@time: 2023/7/30 22:37
@desc:

"""
import requests
from .util_decorate import retry


class NetWorkRequests():

    @retry()
    def _requests(self, url, headers=None, params=None, cookies=None, timeout=5,
                  data=None, json=None, method="POST", stream=False, proxies=None):
        """

        :param url:
        :param headers:
        :param params:
        :param cookies:
        :param timeout:
        :param data:
        :param json:
        :param method:
        :param stream:
        :param proxies:
        :return:
        """
        if method == "POST":
            response = requests.post(
                url=url,
                data=data,
                json=json,
                stream=stream,
                cookies=cookies,
                headers=headers,
                proxies=proxies,
                timeout=timeout)
        elif method == "GET":
            response = requests.get(
                url=url,
                params=params,
                stream=stream,
                cookies=cookies,
                headers=headers,
                proxies=proxies,
                timeout=timeout)
        else:
            raise ValueError("ERROR Methods {}".format(method))
        return response

    def download_videos(self, url, file, max_size=102400, method="GET"):
        """

        :param url:
        :param file:
        :param max_size:
        :param method:
        :return:
        """
        res = {
            "status":-1,
            "url":url,
            "msg":"Failed",
            "result":[]
        }
        response = self._requests(url, method=method)
        if response["status"] == -1:
            msg = "Failed"
        else:
            download_status_all = True
            content_len = int(response["msg"].headers["Content-Length"])
            nums = content_len // max_size if content_len % max_size == 0 else content_len // max_size + 1
            res["content_len"] = content_len
            res["split_nums"] = nums
            with open(file, "ab+")as fp:
                for i in range(nums):
                    start, end = i * max_size, (i + 1) * max_size
                    headers = {
                        "Range": "bytes={}-{}".format(start, end)
                    }
                    response = self._requests(
                        url, headers=headers, stream=True, method=method)
                    chunk_status = response["status"]
                    if chunk_status == -1:
                        download_status_all = False
                    else:
                        for chunk in response["msg"].iter_content(chunk_size=max_size):
                            fp.seek(start)
                            fp.write(chunk)
                    res["result"].append({
                        "status":chunk_status,
                        "during":[start,end],
                    })
            if download_status_all:
                msg = "Success"
                res["status"] = 0
            else:
                msg = "Failed"

        res["msg"] = msg

        return res

你可能感兴趣的:(无用良品,python,开发语言,linux)