本篇文章给大家分享的是有关怎么在python3中使用flask编写一个post接口,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
代码如下:
#!/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接口,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注天达云行业资讯频道。