-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie.py
49 lines (37 loc) · 1.33 KB
/
cookie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from flask import Flask, request, redirect, url_for, session
app = Flask(__name__)
app.secret_key = 'my_secret_key'
users = {
'admin': {'password': 'admin123'},
'user1': {'password': 'password1'},
'user2': {'password': 'password2'}
}
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if username in users and users[username]['password'] == password:
session['username'] = username
return redirect(url_for('profile'))
else:
return 'Неверные учетные данные. <a href="/login">Попробовать снова</a>'
return '''
<form method="post">
<p><input type=text name=username>
<p><input type=password name=password>
<p><input type=submit value=Login>
</form>
'''
@app.route('/profile')
def profile():
if 'username' in session:
return f'Профиль пользователя: {session["username"]}. <a href="/logout">Выйти</a>'
else:
return redirect(url_for('login'))
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('login'))
if __name__ == '__main__':
app.run(port=5000, debug=True)