用python实现requests请求封装

https://blog.csdn.net/weixin_45912307/article/details/109517517

import requests
import json
import logging
from apitest.resquestsTests import datas_tests


class HttpRequest:
    def http_request(self, method, url, param, header):
        global res
        if method.upper() == "POST":
            if header == "Content-type:multipart/form-data":
                try:
                    res = requests.post(url=url, data=param)
                    logging.info('post的form-data数据类型请求...')
                except Exception as e:
                    logging.error('post的form-data数据类型请求异常为:', e)
            if header == "Content-type:application/json":
                try:
                    res = requests.post(url=url, json=param)
                    logging.info('post的json数据类型请求...')
                except Exception as e:
                    logging.error('post的json数据类型请求异常为:', e)
            if header == "Content-type:application/x-www-form-urlencoded":
                try:
                    res = requests.post(url, data=param)
                    logging.info('post请求的x-www-form-urlencoded数据类型请求...')
                except Exception as e:
                    logging.error('post的x-www-form-urlencoded数据类型请求异常为:', e)
        if method.upper() == 'GET':
            if header is not None:
                try:
                    res = requests.get(url=url, data=param, headers=header)
                    logging.info('带请求头的get请求...')
                except Exception as e:
                    logging.error('带请求头的get请求异常为:', e)
            else:
                try:
                    res = requests.get(url=url, data=param)
                    logging.info('无请求头的get请求...')
                except Exception as e:
                    logging.error('无请求的get请求异常为:', e)
        return json.dumps(res.json(), ensure_ascii=False, sort_keys=True, indent=4)

# if __name__ == '__main__':
#     print(requestsUtils().post_main(method=datas_tests.post_method,url=datas_tests.test_url+datas_tests.get_lujing,
#                               data={"userid":datas_tests.uid,"activityid":datas_tests.activityid},
#                                     header=datas_tests.header))
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值