[Flask Project] Project Preparation – Use of Ronglian Cloud SMS Service Platform – Send SMS, Flask-limiter Current Limit

Hits: 0

SDK Reference (New Version)

The cloud communication platform provides developers with a multi-language SDK, so that you can easily choose the programming language you are familiar with to experience. [The source code]
of the new SDK for SMS service has been hosted on the open source platform Github. You can call the cloud communication SMS [API] through the SDK . You can obtain relevant guidelines and SDK source code in the following ways:[]
[]

If the python language is used, find the SDK related files. I
want to put the SDK files into the tool utils under the public method of the project

SendMessage.py file

Execute the main function: the verification code is sent successfully, and the mobile phone receives the verification code

User registration (backend)

The user obtains the SMS verification code code

class User_SMS(Resource):
    """
    Send SMS with verification code,
    """ 
    def  get (self) : 
        mobile=request.args.get( 'mobile' )
         #random verification code 
        code=random.randint( 1000 , 9999 )

        result=send_message(tid= '1' ,mobile = mobile,datas = (code, '1' ))
         #result is a string in json format, which needs to be converted into a dictionary
        result=json.loads(result)
        # Add the mobile phone number to the response 
        result[ 'mobile' ] = mobile
         #After the SMS verification code is sent successfully, the verification code needs to be stored in the fangdaoredis database so that it can be used again

        return result

Request interface: send successfully

[Flask] -limiter current limiter

Flask-Limiter provides rate limiting features for flask routing . It supports backend implementations using memory, redis or memcache as storage environments.

Note:
1. You cannot send text messages repeatedly.
2. The content of the SMS verification code needs to be saved: redis

Install:

pip install flask-limiter

Directory Structure:

limiter.py file:

from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

#Create a current 
limiter limiter=Limiter(key_func=get_remote_address)

constants.py file

#define current limiter constant

#Limit the frequency of SMS verification code sending according to mobile phone number 
LIMIT_SMS_CODE_BY_MOBILE = '1/minute'

#According to the client ip to limit the frequency of SMS verification code sending 
LIMIT_SMS_CODE_BY_IP = '30/hour'

#The time limit of SMS verification code stored in the redis database is 
SMS_CODE_EXPIRES = 20 * 60

init.py file under resources

Place the blueprint of the project in the #shop directory 
from flask import Flask
 from settings.default import map_config
 from shop.rescoures.user import user_bp
 from shop.rescoures.order import order_bp


def app_config(config_type):
    app=Flask(__name__)
    app.config.from_object(map_config.get(config_type))

    #Initialize SQLALchemy 
    from comment.models import db
    db.init_app(app)

    #Initialize the current limiter 
    from comment.utils.limiter import limiter
    limiter.init_app(app)

    # Load log processing tools 
    from comment.utils.loggings import create_logger
    create_logger(app)

    #Register blueprint 
    app.register_blueprint(user_bp,url_prefix= '/user' )
    app.register_blueprint(order_bp,url_prefix='/order')
    return app

user_resoures.py

#define blueprint view

from flask_restful import Resource

#Load User model class

from flask import current_app,request
 from comment.utils.SendMessage import send_message
 import random
 import json
 from comment.utils.limiter import limiter
 from shop.rescoures.user import constants
 from flask_limiter.util import get_remote_address #Define
 resource 
class  Shoping (Resource) : 
    def  get (self) : 
        current_app.logger.info( 'My test log' )
         #The code here may use the User model class 
        return {"hello":"test"}


class User_SMS(Resource):
    """
    Send SMS with verification code,
    """
    error_message='TO many requests.'
    decorators = [
        #3 parameters: parameter 1: rate of current limit; parameter 2: key_func; parameter 3: information after exceeding the limit
        limiter.limit(constants.LIMIT_SMS_CODE_BY_MOBILE,
                      key_func=lambda :request.args['phone'],
                      error_message=error_message),
        limiter.limit(constants.LIMIT_SMS_CODE_BY_MOBILE,
                      key_func=get_remote_address,
                      error_message=error_message),
    ]

    def  get (self) : 
        mobile=request.args.get( 'mobile' )
         #random verification code 
        code=random.randint( 1000 , 9999 )

        result=send_message(tid= '1' ,mobile = mobile,datas = (code, '1' ))
         #result is a string in json format, which needs to be converted into a dictionary
        result=json.loads(result)
        # Add the phone number to the response 
        result[ 'mobile' ] = mobile

        return result

Leave a Reply

Your email address will not be published.