# -*- coding: utf-8 -*-
# 日志系统
# 时间:2017-08-31
# 姓名:xx
import logging
import os
from datetime import datetime
class MainLog:
def __init__(self):
pass
@staticmethod
def getLog():
logging.basicConfig(
# filename=MainLog.logTxt(), # 日志输出到文件
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
return logging
@staticmethod
def logTxt():
today_name = datetime.today().strftime("%Y%m%d")
log_name = datetime.today().strftime("%Y%m%d%H%M%S") + ".txt"
# 新建当天的文件夹路径
path = "F:\\Logs\\" + today_name + "\\"
if os.path.exists(path):
pass
else:
os.makedirs(path)
# 日志文件路径名称
file_path = path + log_name
return file_path
=======================
# -*- coding: utf_8 -*-
# 基础接口请求方法
# 时间:2017-08-31
import requests
from requests.exceptions import ReadTimeout, ConnectionError, RequestException
from Log.main_log import MainLog
class MainResponse:
def __init__(self):
pass
@staticmethod
def mainPost(url=None, params=None, json=None):
u"""
url: 接口请求的url
params: 接口请求的请求头
json: 接口请求的json
"""
logger = MainLog.getLog()
try:
response = requests.post(url=url, params=params, json=json, timeout=10)
code = response.status_code
text = response.text
logger.debug(u"接口请求地址:")
logger.debug(url)
logger.debug(u"接口请求头:")
logger.debug(params)
logger.debug(u"接口请求参数:")
logger.debug(json)
logger.debug(u"接口请求返回码:")
logger.debug(code)
logger.debug(u"接口返回信息:")
logger.debug(text)
return text
except ReadTimeout:
logger.error(u"请求超时")
except ConnectionError:
logger.error(u"请求连接错误")
except RequestException:
logger.error(u"返回错误")
原文:https://www.cnblogs.com/oracle614/p/11848328.html