
这里处理外部请求的例子
from settings import XXX_HOSTimport jsonimport requestsfrom ...views import loggerimport tracebackclass xxSystem_api():def __init__(self):self.host = XXX_HOST # '127.0.0.1:8000'self.api_name = "/xx/xx/"self.time_out = 60def get_xx(self):try:'''curl --location '127.0.0.1:8000/xx/xx/' \--header 'Content-Type: application/json' \--data '{"t1":"1",}'''''''"data": 123123,'''url = self.host + self.api_namelogger.info(url) # 记录请求urldata = {"t1":"1",}logger.info(data) # 记录请求数据header_dict = {'Content-Type': 'application/json'}resp = requests.post(url=url, data=json.dumps(data), timeout=self.time_out, headers=header_dict)respJson = json.loads(resp.text)return_code = respJson['code']return_data = respJson['data']logger.info('XXX_api,xx功能,成功')return return_dataexcept Exception as e:traceback.print_exc# logger.error是给开发人员看得logger.error(url)logger.error(data)logger.error(return_code)logger.error(return_data)logger.error('XXX_api,xx功能,失败')logger.error(e)# 返回是给用户看的raise ValueError("XXX_api,xx功能,失败")if __name__ == "__main__":# 直接跑这个.py文件,直接查看返回信息,不需要额外postman之类的xx = xxSystem_api()data = xx.get_xx()print(data)