Flask实现简单的web后端登录注册

最近学习了如何用flask 迅捷搭建服务器
在这里插入图片描述
其中app.py是flask web后端
postman是模拟前端发送post请求
database.db是sqlite3
数据库文件

  • app.py

from flask import Flask
from flask import request
import sqlite3
app = Flask(__name__)

def usrcheck(username):
    conn = sqlite3.connect('database.db')
    c = conn.cursor()
    res = c.execute('select uid from usr where username=' + '\'' + username + '\'')
    res = res.fetchone()
    conn.close()
    if res == None:
        return -1
    else:
        return res[0]

def logincheck(username,password):
    conn = sqlite3.connect('database.db')
    c = conn.cursor()
    res = c.execute('select uid from usr where username=' + '\'' + username + '\'' + ' and password=' + '\'' + password + '\'')
    res = res.fetchone()
    conn.close()
    if res == None:
        return -1
    else:
        return res[0]

def dbRegister(username,password):
    conn = sqlite3.connect('database.db')
    c = conn.cursor()
    res = c.execute('select max(uid) from usr')
    res = res.fetchone()
    uid = int(res[0]) + 1
    res = c.execute('insert into usr("uid","username","password") values({},\'{}\',\'{}\')'.format(uid,username,password))
    conn.commit()
    conn.close()
    return uid


@app.route("/")
def root():
    return 'Oh You reach the root directory,Awesome!' 

@app.route('/login',methods=['POST'])
def login():
    username = request.json['username']
    password = request.json['password']
    dic = {}
    dicinfo = {}
    check = usrcheck(username)
    if(check == -1):
        dic['status'] = 'fail'
        dicinfo['raw'] = 'login failed. user does\'t exist!'
        dicinfo['uid'] = ''
        dic['info'] = dicinfo
        return dic
    check = logincheck(username,password)
    if(check != -1):
        dic['status'] = 'success'
        dicinfo['raw'] = 'login success'
        dicinfo['uid'] = check
        dic['info'] = dicinfo
    else:
        dic['status'] = 'fail'
        dicinfo['raw'] = 'login failed. password is wrong!'
        dicinfo['uid'] = ''
        dic['info'] = dicinfo
    return dic
@app.route('/register',methods=['POST'])
def register():
    username = request.json['username']
    password = request.json['password']
    dic = {}
    dicinfo = {}
    check = usrcheck(username)
    if(check != -1):
        dic['status'] = 'fail'
        dicinfo['raw'] = 'register failed. user already exist!'
        dicinfo['uid'] = ''
        dic['info'] = dicinfo
        return dic
    uid = dbRegister(username,password)
    dic['status'] = 'success'
    dicinfo['raw'] = 'register success'
    dicinfo['uid'] = uid 
    dic['info'] = dicinfo
    return dic
  • postman.py

import requests
import json
def post(url,sendDict):
    return requests.post(url = url,json = sendDict).content

def testLogin(username,password):
    url = 'http://127.0.0.1:5000/login'
    sendDict = {'username':username,'password':password}
    result = post(url,sendDict)
    print('JSON:')
    print(result)
    result_json = json.loads(result)
    print('Parser Info:')
    print(result_json['info']['raw'])
    print('')

def testRegister(username,password):
    url = 'http://127.0.0.1:5000/register'
    sendDict = {'username':username,'password':password}
    result = post(url,sendDict)
    print('JSON:')
    print(result)
    print('')

def main():
    testLogin('ybx','nb')
    testLogin('admin','admin')
    testLogin('ybxx','nb')
    testLogin('ybx','nbb')

    testRegister('zk','nb')
    testRegister('ybx','newpass')

if __name__ == '__main__':
    main()
  • sql

在这里插入图片描述
转储的sql文件:
在这里插入图片描述

/*
 Navicat SQLite Data Transfer

 Source Server         : test
 Source Server Type    : SQLite
 Source Server Version : 3030001
 Source Schema         : main

 Target Server Type    : SQLite
 Target Server Version : 3030001
 File Encoding         : 65001

 Date: 24/07/2021 15:03:21
*/

PRAGMA foreign_keys = false;

-- ----------------------------
-- Table structure for usr
-- ----------------------------
DROP TABLE IF EXISTS "usr";
CREATE TABLE "usr" (
  "uid" INTEGER NOT NULL,
  "username" TEXT NOT NULL,
  "password" TEXT NOT NULL,
  PRIMARY KEY ("uid")
);

-- ----------------------------
-- Records of usr
-- ----------------------------
INSERT INTO "usr" VALUES (0, 'admin', 'admin');
INSERT INTO "usr" VALUES (1, 'ybx', 'nb');
INSERT INTO "usr" VALUES (2, 'dzp', 'sssp');
INSERT INTO "usr" VALUES (3, 'testname', 123456);

PRAGMA foreign_keys = true;

项目执行:

flask run
#cd到app.py路径下,直接启动flask
flask run --host=0.0.0.0
#部署到服务器上,供外网访问

效果

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值