一、python封装Httpclient模式:
import requests
import urllib3
class HttpClient:
"""Generic Http Client class"""
def __init__(self, disable_ssl_verify=False, timeout=60):
"""Initialize method"""
self.client = requests.session()
self.disable_ssl_verify = disable_ssl_verify
self.timeout = timeout
if self.disable_ssl_verify:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def Get(self, url, headers=None, data=None, json=None, params=None, *args, **kwargs):
"""Http get method"""
if headers is None:
headers = {}
if self.disable_ssl_verify:
response = self.client.get(url, headers=headers, data=data, json=json, params=params
, verify=False, timeout=self.timeout, *args, **kwargs)
else:
response = self.client.get(url, headers=headers, data=data, json=json, params=params
, timeout=self.timeout, *args, **kwargs)
response.encoding = 'utf-8'
print(f'{response.json()}')
return response
def Post(self, url, headers=None, data=None, json=None, params=None, *args, **kwargs):
"""Http get method"""
if headers is None:
headers = {}
if self.disable_ssl_verify:
response = self.client.post(url, headers=headers, data=data, json=json, params=params
, verify=False, timeout=self.timeout, *args, **kwargs)
else:
response = self.client.post(url, headers=headers, data=data, json=json, params=params
, timeout=self.timeout, *args, **kwargs)
response.encoding = 'utf-8'
print(f'{response.json()}')
return response
二、python直接请求requests模块封装:
import requests
import traceback
from LogUtil import Logger
from settings import *
class RunMethod(object):
def __init__(self):
self.log = logging
def post(self, url, headers, data=None, json=None, params=None):
requests.packages.urllib3.disable_warnings()
try:
response = requests.post(url=url, headers=headers, data=data, json=json, params=params, verify=False)
self.log.info(
f"\n\t----------{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}接口请求开始---------" + f" \n\t 接口请求方式:Post \n\t 接口请求路径:{url} \n\t 接口请求头:{headers} \n\t 接口请求参数:json:{json} \n\t 接口响应结果:{response.json()}")
return response
except Exception as e:
self.log.error(
f"\n\t----------{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}接口请求异常---------" + f" \n\t 接口请求方式:Post \n\t 接口请求路径:{url} \n\t 接口请求头:{headers} \n\t 接口请求参数:json:{json} \n\t 接口响应结果:{response.json()}" + f" \n\t 接口响应异常信息:{e}" + str(
traceback.format_exc()))
def get(self, url, headers=None, data=None, json=None, params=None):
requests.packages.urllib3.disable_warnings()
try:
response = requests.get(url=url, headers=headers, data=data, json=json, params=params, verify=False)
self.log.info(
f"\n\t----------{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}接口请求开始---------" + f" \n\t 接口请求方式:Get \n\t 接口请求路径:{url} \n\t 接口请求头:{headers} \n\t 接口请求参数:data:{data} \n\t 接口响应结果:{response}")
return response
except Exception as e:
self.log.error(
f"\n\t----------{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}接口请求异常---------" + f" \n\t 接口请求方式:Get \n\t 接口请求路径:{url} \n\t 接口请求头:{headers} \n\t 接口请求参数:data:{data} \n\t 接口响应结果:{response}" + f" \n\t 接口响应异常信息:{e}" + str(
traceback.format_exc()))