flask 添加用户认证API , python 客户端用户认证

flask 添加用户认证API , python 客户端用户认证

flask blog 演示项目

Documentation = https://flask.palletsprojects.com/tutorial/

中文Documentation = https://flask.net.cn/

源码 https://github.com/pallets/flask/tree/main/examples/tutorial

修改 auth.py 添加用户认证API

python 复制代码
### web 访问授权验证 
@bp.route("/author", methods=("GET", "POST"))
def author():
    """author a user is registered or not"""

    ret = -1

    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]
        db = get_db()
        
        user = db.execute(
            "SELECT * FROM user WHERE username = ?", (username,)
        ).fetchone()

        if user is None:
            ret = 1 # "Incorrect username."
        elif not check_password_hash(user["password"], password):
            ret = 2 #"Incorrect password."

        if ret is -1:
            ret = 0  # "author OK"

    jsonRet={
        "status":ret,
        "value":ret
    }

    return jsonRet 

client.py 访问 网站 获取认证结果

其中 url = "http://127.0.0.1:5000/auth/author" 替换为实际的 flask服务器地址

我这里在本机演示,所以是"http://127.0.0.1:5000/auth/author"

python 复制代码
import requests
import json
from typing import Tuple

import getpass

def author(username: str, password: str) -> Tuple[bool, str]:
    # 将此URL替换为您要登录的网站的URL
    url = "http://127.0.0.1:5000/auth/author"
    payload = {
        "username": username,
        "password": password
    }
    
    response :requests.Response = requests.post(url, data=payload, headers={"Referer": url})  # 模拟浏览器登陆过程

    print(f"response.url: {response.url}") 
    print(f"response.content: {response.content}")
    print(f"response.status_code: {response.status_code}")

    json_data = json.loads(response.content)
    status = json_data["status"]
    value = json_data["value"]
    print(f"status : {status}")
    print(f"value : {value}")

    if status == 0:
        return {True,"author OK"}
    else:
        return {False,"author Error"}

username = input("请输入用户名:")
# password = input("请输入密码:")
password = getpass.getpass("请输入密码:")

ret,message = author(username, password)
print(f"{ret} {message}")

测试

我这网站用户提前添加

用户名 wmx

密码 123

测试,运行client.py

wmx 123 验证正确:

bash 复制代码
请输入用户名:wmx
请输入密码:
response.url: http://127.0.0.1:5000/auth/author
response.content: b'{\n  "status": 0,\n  "value": 0\n}\n'
response.status_code: 200
status : 0
value : 0
True author OK

ad ad 验证失败:

bash 复制代码
请输入用户名:ad
请输入密码:
http://127.0.0.1:5000/auth/author
b'{\n  "status": 1,\n  "value": 1\n}\n'
200
status : 1
value : 1
False author Error
相关推荐
m0_733565461 分钟前
JavaScript中Reflect-ownKeys获取所有键名的优势
jvm·数据库·python
水木流年追梦15 分钟前
大模型入门-应用篇3-Agent智能体
开发语言·python·算法·leetcode·正则表达式
IT_陈寒27 分钟前
为什么Java的Stream并行处理反而变慢了?
前端·人工智能·后端
财经资讯数据_灵砚智能34 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月12日
人工智能·python·信息可视化·自然语言处理·ai编程
2301_815901971 小时前
C#怎么使用协变和逆变 C#泛型中的in和out关键字协变逆变是什么意思怎么用【语法】
jvm·数据库·python
孙6903421 小时前
swf 图片转 pdf
java·后端
Pkmer1 小时前
LeetCode 上极少见的工程级滑窗实现
python·leetcode
m0_463672201 小时前
SQL优化SQL关联查询中的排序字段_减少临时空间占用与内存开销
jvm·数据库·python
FreakStudio1 小时前
开源分享|用MicroPython 做了个 AI 小鸡,它会长大,还记得我所有的情绪
python·单片机·嵌入式·面向对象·并行计算·电子diy·电子计算机
长安不见1 小时前
从CompletionService的一个错误用法谈起
后端