最近研究了一下python的django框架, 发现这个框架不比Java spring boot差, mock同样一个接口, 代码量少很多, 维护起来也很方便, 废话不多说,直接上代码
1. 安装django: pip install django
2. 创建项目: django-admin startproject postdemo
3. 启动项目: python manage.py runserver 0.0.0.0:8001
启动Django项目就这么简单, 三步完成, O(∩_∩)O
postdemo 包下创建views.py
1 from django.http import HttpResponse 2 3 from .utils import process_parameters, validate_parameters, read_response 4 5 6 def postdemo(request): 7 if request.method == ‘POST‘: 8 dic_params = process_parameters(request.body.decode(‘utf-8‘)) # 一定要用utf-8解码,不然传入的是bytes类型b‘body‘ 9 if validate_parameters(dic_params): 10 result = read_response() 11 res = HttpResponse(result) 12 # res.__setitem__("resultcode", ‘0‘) # 用这个也OK, __setitem__可以修改默认的header, 比如Content-Type, setdefault不行 13 res.setdefault(‘resultcode‘, ‘0‘) 14 res.setdefault(‘msg‘, ‘no error‘) 15 return res 16 else: 17 return HttpResponse(‘error username or password!‘) 18 else: 19 return HttpResponse(‘error method!‘)
postdemo 包下创建utils.py, 定义业务逻辑方法, 采用函数式编程也可以
1 import configparser 2 import json 3 4 5 def read_config(): 6 config = configparser.ConfigParser() 7 dic = {} 8 config.read("./config.ini", encoding="utf8") 9 config.sections() 10 dic["username"] = config.get("login", "username") 11 dic["password"] = config.get("login", "password") 12 return dic 13 14 15 def read_response(): 16 config = configparser.ConfigParser() 17 dic = {} 18 config.read("./config.ini", encoding="utf8") 19 config.sections() 20 dic["nickname"] = config.get("response_data", "nickname") 21 dic["sex"] = config.get("response_data", "sex") 22 dic["age"] = config.get("response_data", "age") 23 dic["status"] = config.getint("response_data", "status") 24 dic["uid"] = config.get("response_data", "uid") 25 return json.dumps(dic, ensure_ascii=False) 26 27 28 def process_parameters(body): 29 try: 30 body_dic = {} 31 key_values = str(body).split("&") 32 for key_value in key_values: 33 body_dic[key_value.split("=")[0]] = key_value.split("=")[1] 34 return body_dic 35 except Exception as e: 36 return e 37 38 39 def validate_parameters(body): 40 if isinstance(body, dict): 41 dic_config = read_config() 42 if dic_config["username"] == body["username"] and dic_config["password"] == body["password"]: 43 return True 44 else: 45 return False
postdemo包平级目录创建配置文件config.ini
[login] username=123456789@qq.com password=123456 [response_data] nickname=旦增尼玛 sex=1 age=18 status=1 uid=123456789
默认文件urls.py, 添加path
1 from django.contrib import admin 2 from django.urls import path 3 4 from . import views 5 6 urlpatterns = [ 7 path(‘admin/‘, admin.site.urls), 8 path(‘postdemo‘, views.postdemo), 9 ]
默认文件setting.py, 注释掉中间件的csrf安全验证, 其他默认
1 MIDDLEWARE = [ 2 ‘django.middleware.security.SecurityMiddleware‘, 3 ‘django.contrib.sessions.middleware.SessionMiddleware‘, 4 ‘django.middleware.common.CommonMiddleware‘, 5 # ‘django.middleware.csrf.CsrfViewMiddleware‘, 6 ‘django.contrib.auth.middleware.AuthenticationMiddleware‘, 7 ‘django.contrib.messages.middleware.MessageMiddleware‘, 8 ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘, 9 ]
到这里就完了, 一个简单的post 接口就完成了, 如果还有进行cookie等校验, 完善utils.py业务处理逻辑即可, 启动项目
原文地址:https://www.cnblogs.com/andrew209/p/9739700.html
时间: 2024-10-25 13:09:11