Python requests库请求封装

需求

传入参数后自动判定方法类型、方法参数格式化进行请求访问

代码

import json
import requests


def str_to_json(data):
    if data is None:
        return data
    try:
        return json.loads(data)
    except Exception as e:
        return data


def requester(flow):
    url = flow["url"]
    method = flow["method"]
    headers = flow["headers"]
    post_data = flow["post_data"]
    proxy = flow["proxy"]

    try:
        if method == "POST":
            if headers["content-type"] and "application/json" in headers["content-type"]:
                r = requests.post(
                    url=url,
                    headers=headers,
                    json=str_to_json(post_data),
                    proxies=proxy,
                    verify=False
                )
            else:
                r = requests.post(
                    url=url,
                    headers=headers,
                    data=post_data,
                    proxies=proxy,
                    verify=False
                )
        elif method == "GET":
            r = requests.get(
                url=url,
                headers=headers,
                proxies=proxy,
                verify=False
            )
        else:
            print("不支持的方法: {}, url: {}".format(method, url))
    except Exception as e:
        print("请求 {} 错误: {}".format(url, e))
        return None
    return r

测试



if __name__ == "__main__":

    request_datas = [
        {
            #"request_url": "http://172.16.12.133:5888/api/shop/order/detail?order_id=9&_signature=11111&aaa=888",
            "request_url": "http://172.16.12.133:5888/api/shop/product/buy",
            "resource_type": "document",
            #"request_method": "GET",
            "request_method": "POST",
            #"request_post_data": "{\"id\":1,\"first_name\":\"gesilaadmin\",\"surname\":\"gesilaadmin\"}",
            "request_post_data": "{\"product_id\":\"4\",\"coupon_code\":\"dbe9fc5ba2e2da23acdae64dc5a2b2f2\"}",
            "requset_headers": {
                "accept": "Accept: */*",
                "accept-encoding": "gzip, deflate",
                "accept-language": "zh-CN,zh;q=0.9",
                "content-type": "application/json",
                "Origin": "http://172.16.12.133:5888",
                "Referer": "http://172.16.12.133:5888/checkout?product_id=4",
                "connection": "close",
                "cookie": "session=eyJ1c2VybmFtZSI6ImFkbWluIn0.Zb8-BA.3DX4qB1yNha7w3hz1R3_BedIZ_g",
                "host": "http://172.16.12.133:5888",
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
                "Authorization": "Bareer 11111"
            }
        }
    ]


    flow = {
            "url": data["request_url"],
            "headers": data["requset_headers"],
            "method": data["request_method"],
            "post_data": data["request_post_data"],
            "query": get_url_query(data["request_url"]),
            "proxy": None,
            "cookie2": cookie2,
            "matchreplace": matchreplace
    }

    r = requester(flow)
    if not r:
        print("请求出错")
    else:
        print(r.text)

相关推荐

  1. Python requests请求封装

    2024-02-07 08:58:02       56 阅读
  2. HarmonyOS 网络请求工具封装,直接无脑用!!!

    2024-02-07 08:58:02       40 阅读
  3. 【前端开发】Uniapp:uView组件封装接口请求

    2024-02-07 08:58:02       34 阅读
  4. 请求封装(axios、fetch)

    2024-02-07 08:58:02       59 阅读
  5. pytest封装请求

    2024-02-07 08:58:02       47 阅读
  6. 封装promise请求方式

    2024-02-07 08:58:02       48 阅读
  7. uniapp 接口请求封装

    2024-02-07 08:58:02       29 阅读
  8. uniapp 请求封装

    2024-02-07 08:58:02       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-07 08:58:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-07 08:58:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-07 08:58:02       87 阅读
  4. Python语言-面向对象

    2024-02-07 08:58:02       96 阅读

热门阅读

  1. 力扣283.移动零

    2024-02-07 08:58:02       55 阅读
  2. css基础

    css基础

    2024-02-07 08:58:02      53 阅读
  3. 2024年笔记--centos docker离线安装启动失败

    2024-02-07 08:58:02       57 阅读
  4. 10分钟快速入门正则表达式

    2024-02-07 08:58:02       48 阅读