zoukankan      html  css  js  c++  java
  • 使用requests模块进行封装,帮你如何处理restful类型的接口

    import requests
    import urllib3
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
    
    
    class HandleRequests:
    
        def __init__(self):
            self.one_session = requests.Session()
    
        def request(self, url, method='get', headers=None, param=None, data=None, is_json=False, is_http=False):
            '''
            定义一个请求方法
            :param url: 域名接口
            :param method: 请求方法
            :param headers: 请求头
            :param param: get请求体
            :param data: post表单请求体
            :param is_json: 是否为json请求数据
            :param is_http: 是否为http请求
            :return: 请求结果
            '''
            if is_http is False:
                if method.lower() == 'get':
                    res = self.one_session.get(url=url, headers=headers, params=param, verify=False)
                    return res
                elif method.lower() == 'post':
                    if is_json:
                        res = self.one_session.post(url=url, headers=headers, data=data, verify=False)
                        return res
                    else:
                        res = self.one_session.post(url=url, headers=headers, json=data, verify=False)
                        return res
                elif method.lower() == 'delete':
                    res = self.one_session.delete(url=url, headers=headers, verify=False)
                    return res
                else:
                    print("不支持{}请求方法!".format(method))
            else:
                if method.lower() == 'get':
                    res = self.one_session.get(url=url, headers=headers, params=data)
                    return res
                elif method.lower() == 'post':
                    if is_json:
                        res = self.one_session.post(url=url, headers=headers, data=data)
                        return res
                    else:
                        res = self.one_session.post(url=url, headers=headers, json=data)
                        return res
                elif method.lower() == 'delete':
                    res = self.one_session.delete(url=url, headers=headers)
                    return res
                else:
                    print("不支持{}请求方法!".format(method))
    
        def close_cookie(self):
            '''
            关闭cookie
            '''
            self.one_session.close()
  • 相关阅读:
    Android模拟器中访问局域网地址
    wifidirect
    asp.net上传图片自动生成缩略图功能代码
    Asp.Net二级域名共享Forms身份验证、下载站/图片站的授权访问控制
    最简单的三层实例【插入据
    使用RGBa和Filter实现不影响子元素的CSS透明背景
    Jquery ui widget开发
    SQL优化
    SQL SERVER 数据类型
    SQL Server数据库查询速度慢的原因
  • 原文地址:https://www.cnblogs.com/xiaogongjin/p/11372155.html
Copyright © 2011-2022 走看看