You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
665 B
28 lines
665 B
|
5 years ago
|
import time
|
||
|
|
import redis
|
||
|
|
from flask import Flask
|
||
|
|
|
||
|
|
app = Flask(__name__)
|
||
|
|
cache = redis.Redis(host='redis', port=6379)
|
||
|
|
|
||
|
|
# fonction de récupération et incrémentation du compteur stocké dans redis
|
||
|
|
def get_hit_count():
|
||
|
|
retries = 5
|
||
|
|
while True:
|
||
|
|
try:
|
||
|
|
return cache.incr('hits')
|
||
|
|
except redis.exceptions.ConnectionError as exc:
|
||
|
|
if retries == 0:
|
||
|
|
raise exc
|
||
|
|
retries -= 1
|
||
|
|
time.sleep(0.5)
|
||
|
|
|
||
|
|
|
||
|
|
@app.route('/')
|
||
|
|
def hello():
|
||
|
|
count = get_hit_count()
|
||
|
|
return 'Hello YOURNAME ! I have been seen {} times.\n'.format(count)
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
app.run(host="0.0.0.0", debug=True)
|