• Http类


    #coding:utf-8
    from urllib.error import URLError
    
    import requests
    import common.Log as Log
    import ssl
    import urllib
    import os
    '''
    @author: jinff
    @license: (C) Copyright 2016-2020, 中国制造
    @contact:
    @file: HttpUtil.py
    @time: 2018/10/25 11:05
    @desc: HttP工具类
    '''
    
    log = Log.getLogger()
    
    
    requests.packages.urllib3.disable_warnings()
    requests.adapters.DEFAULT_RETRIES = 5
    session = requests.Session()
    session.keep_alive = False
    #session.proxies = {"https": "111.231.140.109:8888", "https": "111.231.140.109:8888" }
    
    
    
    
    def doPost(**kwargs):
    
    
        if kwargs["headers"] is None:
            kwargs["headers"]= {'Content-Type': 'application/json;charset=UTF-8'}
    
    
        if "data" in kwargs:
            body=kwargs["data"]
    
            # data参数校验
            if isinstance(kwargs["data"], str):
                kwargs["data"] = kwargs["data"].encode('utf-8')
    
            elif isinstance(kwargs["data"], bytes):
                pass
            else:
    
                raise TypeError("data类型错误:" + kwargs["data"])
        elif "json" in kwargs:
            body=kwargs["json"]
        else:
            body=None
    
    
    
        kwargs["verify"]=False
    
        log.debug("进入请求post方法:")
        log.debug("url= %s
    body=%s
    headers=%s" %(kwargs['url'],body,kwargs["headers"]))
     # 如果返回ssl.CertificateError错误,可以post请求中增加参数 context=context
        res=session.post(**kwargs)
    
        return res
    
    
    def doGet(url,paras,headers,**kwargs):
    
        log.debug("进入请求Get方法:")
        log.debug("url= %s
    paras=%s
    headers=%s" %(url,paras,headers))
        if paras is None:
            paras=None
        res=session.get(url=url,params=paras,headers=headers,verify=False,**kwargs)
    
        return res
    def download(url,filename):
    
        # 2. 表示忽略未经核实的SSL证书认证
        context = ssl._create_unverified_context()
        request = urllib.request.Request(url)
        response = urllib.request.urlopen(request, context=context)
        content = response.read().decode('utf-8')
        try:
            with open(filename, 'w',encoding='UTF-8' ) as f:
                 f.write(content)
        except IOError:
            log.error("ERROR: 写文件失败! fileName = ",filename)
    
    
    
    
    
     # @staticmethod
     # def doPost(url, jsonBody, headers):
     #     session = requests.Session()
     #     log.info("进入请求post方法:")
     #     log.info("url= %s
    body=%s
    headers=%s" % (url, jsonBody, headers))
     #
     #     res = session.post(url=url, data=jsonBody.encode('utf-8'), headers=headers, verify=False)
     #
     #     return res
  • 相关阅读:
    ant-pro兄弟组件传参dva数据应用
    ant-design-pro 的EditableProTable表格验证调用
    使用ant-design-pro 的EditableProTable赋值报错
    EditableProTable使用+自定义封装
    由浅入深CAS
    Spring Cloud之Feign 转发请求头(header参数)
    线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式
    接口的幂等性
    Feign 调用丢失Header的解决方案
    springboot测试类测试时注入的对象为null
  • 原文地址:https://www.cnblogs.com/peak911/p/11506790.html
Copyright © 2020-2023  润新知