当前位置:主页 > 软件编程 > Python代码 >

python3使用flask编写注册post接口的方法

时间:2021-03-29 09:44:09 | 栏目:Python代码 | 点击:

使用python3的Flask库写了一个接口,封装了很多东西,仅供参考即可!

代码如下:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import re

from flask import request
from flask_restful import Resource

import aes_utils
import mysql_utils
import sqls_user


class Register(Resource):
 """注册"""

 @staticmethod
 def post():
  data = request.get_json()

  phone = data.get('phone')
  passwd = data.get('passwd')

  if not all([phone, passwd]):
   return {'msg': '请求参数缺失!'}, 400

  if not re.match(r'^1[3456789]\d{9}$', phone):
   return {'msg': '手机号格式错误!'}, 400

  if mysql_utils.get_db_data(sqls_user.select_id_by_phone(), phone):
   return {'msg': '该手机号已经被注册!'}, 500

  mysql_utils.execute(sqls_user.register(), phone, aes_utils.encrypt(passwd)) # 执行sql

  return {'msg': '注册成功!'}, 201

您可能感兴趣的文章:

相关文章