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

yipeiwu_com7年前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

以上这篇python3使用flask编写注册post接口的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python数据类型中的“冒号“[::]——分片与步长操作示例

Python数据类型中的“冒号“[::]——分片与步长操作示例

本文实例讲述了Python数据类型中的“冒号“[::]——分片与步长操作。分享给大家供大家参考,具体如下: 例如有如下字符串: string = "welcome to jb51^_...

ubuntu系统下 python链接mysql数据库的方法

进入root 权限下 apt-get install mysql-server apt-get install mysql-client 创建数据库 mysql -u root...

Python之父谈Python的未来形式

6月初,Python之父Guido van Rossum在今天的PyCon US大会上作了名为“Python Language”的演讲。近日,他又接受了IT媒体Infoworld的采访,...

python统计cpu利用率的方法

本文实例讲述了python统计cpu利用率的方法。分享给大家供大家参考。具体实现方法如下: #-*-coding=utf-8-*- import win32pdh import ti...

Python中%是什么意思?python中百分号如何使用?

常见的两种 第一种:数值运算 1 % 3 是指模运算, 取余数(remainder) >>> 7%2 1 # -*- coding: utf-8 -*...