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\nbody=%s\nheaders=%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\nparas=%s\nheaders=%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\nbody=%s\nheaders=%s" % (url, jsonBody, headers)) # # res = session.post(url=url, data=jsonBody.encode('utf-8'), headers=headers, verify=False) # # return res