Flask 用户登录

@blue.route('/student/register/',methods=['POST','GET'])
def student_register():
    if request.method == 'GET':
        return render_template('register.html')
    elif request.method =='POST':
        username= request.form.get('username')

        password = request.form.get('password')
        #  密码加密
        hash_pwd = generate_password_hash(password)
        student = Student()
        student.s_name = username
        student.s_password = hash_pwd

        db.session.add(student)
        db.session.commit()

        return 'Register successful'


@blue.route('/student/login/',methods=['POST','GET'])
def student_login():
    if request.method == 'GET':
        return render_template('student_login.html')
    elif request.method =='POST':
        username= request.form.get('username')

        password = request.form.get('password')

        student= Student.query.filter(Student.s_name.__eq__(username)).first()
        if check_password_hash(student.s_password,password):
            return 'Login successful'

        return '用户名或者密码不正确'

验证  generate_password_hash()

此错误提示为,model 表,未迁移

注册成功,密码加密

封装一个方法,密码加密

import hashlib

class Student:
    def __init__(self, _password=None):
        self._password = _password
    #     raise Exception('Error Action')
    # #  property将函数变成一个属性
    @property
    def password(self):
        return self._password

    #  steter  设置
    @password.setter
    def password(self, value):
        self._password = hashlib.new('md5', value.encode('utf-8')).hexdigest()


if __name__ == '__main__':
    student = Student()
    student.password = '110'
    print(student.password)

设置为:密码不可访问

Flask-发送邮件Flask-mailman 文档入口

扩展应用: Flask-mailman

1. 安装

pip install Flask-Mail
 

    #  邮件配置信息
    MAIL_SERVER = "smtp.139.com"
    MAIL_PORT = 25
    MAIL_USERNAME = 'xxxghui@139.com'
    MAIL_PASSWORD = 'xxxxxxxx'

@blue.route('/sendmail/')
def send_mail():
    msg = Message( "Hello",recipients=["huapenghui@139.com",])
    msg.body = "Flask  就是这样!"
    msg.html = '<h3><a href= https://www.baidu.com >American RealiWay Station Offer Message</a></h3>'
    mail.send(message = msg)

    return '邮件发送成功'

发qq邮箱,会被标记为 垃圾邮件,自己发,自己收。

短信验证码

网易云信平台 ,需要企业注册,

url

添加请求头

import hashlib
import time
import requests

def send_code():
    url='https://api.netease.in/sms/sendcode.action'
    nonce = hashlib.new('sha512',str(time.time()).encode('utf-8')).hexdigest()
    curtime = str(int(time.time()))
    secret= 'f2f839131b19'
    sha1 =  hashlib.sha1()
    sha1.update((secret+nonce+curtime).encode('utf-8'))
    # check_sum = hashlib.new('sha1',str(secret+nonce+curtime).encode('utf-8')).hexdigest()
    check_sum = sha1.hexdigest()
    headers = {
        'Appkey':'70e20855fccfff9c86d0353a5e08b996',
        'Nonce':nonce,
        'CurlTime':curtime,
        'CheckSum':check_sum,
    }
    post_data = {
        # 'moblie':'15735183437',
        'moblie':'18790579029',
    }
    resp = requests.post(url,data=post_data,headers=headers)
    print(resp.content )

if __name__ == '__main__':
    send_code()

添加缓存

from flask_caching import Cache
from flask_mail import Mail
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
migrate = Migrate()
mail = Mail()

#  配置缓存
cache = Cache(
    config ={
   'CACHE_TYPE':'redis'
}
)

def init_ext(app):
    db.init_app(app)
    migrate.init_app(app, db)
    mail.init_app(app)
    cache.init_app(app)

把程序内嵌到程序中

@blue.route('/sendcode/')
def send_code():
    phone = request.args.get('phone')

    username =  request.args.get('username')
    resp = send_verify_code(phone)

    result = resp.json()
    if result.get('code') ==200:
        obj = result.get('obj')
    # username   验证码 存到缓存中
        cache.set(username,obj)
        data = {
            'msg':'ok',
            'status':200,
        }
        # print(resp.json())
        return  jsonify(data)
    data = {
        'msg': 'fail',
        'status': 400,
    }
    return jsonify(data)

注册页面

@blue.route('/student/register/',methods=['POST','GET'])
def student_register():
    if request.method == 'GET':
        return render_template('register.html')
    elif request.method =='POST':
        username= request.form.get('username')

        password = request.form.get('password')

        repassword = request.form.get('Repassword')

        phone = request.form.get('phone')
        code = request.form.get('code')
        cache_code = cache.get('code')
        if code != cache_code:
            return '验证失败'
        #  密码加密
        # hash_pwd = generate_password_hash(password)
        student = Student()
        student.s_name = username
        # student.s_password = hash_pwd
        student.s_password = password
        student.s_phone = phone

        db.session.add(student)
        db.session.commit()

        return 'Register successful'

js添加点击事件,获取验证码,即发送短信验证码

# 发送短信验证码
def send_verify_code(phone):
    url = 'https://api.netease.in/sms/sendcode.action'
    nonce = hashlib.new('sha512', str(time.time()).encode('utf-8')).hexdigest()
    curtime = str(int(time.time()))
    secret = 'f2f839131b19'
    sha1 = hashlib.sha1()
    sha1.update((secret + nonce + curtime).encode('utf-8'))
    # check_sum = hashlib.new('sha1',str(secret+nonce+curtime).encode('utf-8')).hexdigest()
    check_sum = sha1.hexdigest()
    headers = {
        'Appkey': '70e20855fccfff9c86d0353a5e08b996',
        'Nonce': nonce,
        'CurlTime': curtime,
        'CheckSum': check_sum,
    }
    post_data = {
        # 'moblie':'15735183437',
        'moblie': phone ,
    }
    resp = requests.post(url, data=post_data, headers=headers)

    return resp.content

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值