最近学习了如何用flask 迅捷搭建服务器
其中app.py是flask web后端
postman是模拟前端发送post请求
database.db是sqlite3
数据库文件
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
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文件:
/*
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
#部署到服务器上,供外网访问
效果