-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathexample-2.py
53 lines (36 loc) · 1.25 KB
/
example-2.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
50
51
52
53
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import os
import redis
from flask import Flask, session, redirect, escape, request
# Configure the application name with the FLASK_APP environment variable.
app = Flask(__name__)
# Configure the secret_key with the SECRET_KEY environment variable.
app.secret_key = os.environ.get('SECRET_KEY', default=None)
# Connect to Redis with the REDIS_URL environment variable.
store = redis.Redis.from_url(os.environ.get('REDIS_URL'))
@app.route('/')
def index():
if 'username' in session:
username = escape(session['username'])
visits = store.hincrby(username, 'visits', 1)
return '''
Logged in as {0}.<br>
Visits: {1}
'''.format(username, visits)
return 'You are not logged in'
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
return redirect('/')
return '''
<form method="post">
<p><input type=text name=username>
<p><input type=submit value=Login>
</form>
'''
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect('/')