from flask import Flask, url_for, request, redirect, session

app = Flask(__name__)
app.secret_key = 'Your_secret_key$^52@!'

@app.route('/index')
def index():
    if 'username' in session:
        return f'你好，{session.get("username")}'
    return redirect(url_for("login"))

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        session['username'] = request.form['username']
        return redirect(url_for('index'))
    # 当发送GET请求时，页面显示输入框和"登录"按钮
    return '''
    <form method="post">
        <p><input type=text name=username>
        <p><input type=submit value=登录>
    </form>
    '''

if __name__ == '__main__':
    app.run()