flask实现接口

flask实现接口

from flask import Flask, request

# 创建一个 flask 实例,__name__ 是告诉 flask 应用的名字
app = Flask(__name__)
app.debug = True


# url
@app.route("/index.html")
def hello():
    return "hello world"


@app.route('/login.html', methods=['GET', 'POST'])
def login():
    # 如果是 get 请求:认为打开登录页面,将写好的 HTML 代码返回给你
    if request.method == 'GET':
        return """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>用户登录</title>
        </head>

        <body>
            <h1>用户登录</h1>
            <form action="/login.html" method="post">
                <input type="text" name="uname" placeholder="用户名"/><br>
                <input type="password" name="pwd" placeholder="密码"/><br>
                <input type="submit" value="登录"/>
            </form>
        </body>
        </html>
        """
    else:
         username = request.form.get('uname')
         password = request.form.get('pwd')
         if username == 'alq' and password == '1234':
             return """<h2 style="color: green">登录成功</h2>"""
         else:
             return """<h2 style="color: red">用户名或密码不正确</h2>"""


if __name__ == '__main__':
    app.run(host="localhost", port=9999)
posted @ 2024-01-25 00:11  爱新觉罗LQ  阅读(14)  评论(0编辑  收藏  举报