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
相关推荐
维克兜率天1 天前
【维克】ARMA模型:AR与MA的完美结合
后端·restful
北斗落凡尘1 天前
LangGraph 入门实战(2)
python·langchain
是未才1 天前
从输入 URL 到页面返回:DNS、路由、TLS 与 HTTP 完整链路
java·后端·计算机网络
上海安当技术1 天前
半天接入:USBKey RESTful API + C 动态库,Web 和 C/S 两套集成路径实战
前端·后端·restful·集成·usbkey
ttod_qzstudio1 天前
Java 常用语法极简通关(五):类与对象——字段、方法、构造器、this 与 static
java·开发语言·python
DevUI团队1 天前
从“即兴创作”到“规格先行”,华为云码道(CodeArts)代码智能体持续深耕企业级规范驱动开发能力
前端·人工智能·后端
jufeng13071 天前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 1 篇】
人工智能·python·架构·agent
weixin_431600441 天前
NestJS 入门(3):Guard 如何挡住未登录请求?
前端·后端·学习·nest.js
月光船幽幽1 天前
影子模式下保护 logits 不被修改
人工智能·python·算法
IT_陈寒1 天前
JavaScript类型转换把我坑惨了,这破玩意真该早点搞明白
前端·人工智能·后端