Guest User

Untitled

a guest
Oct 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. from flask import Flask
  2. from redis import Redis, RedisError
  3. import os
  4. import socket
  5.  
  6. # Connect to Redis
  7. redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
  8.  
  9. app = Flask(__name__)
  10.  
  11. @app.route("/")
  12. def hello():
  13. try:
  14. visits = redis.incr("counter")
  15. except RedisError:
  16. visits = "<i>cannot connect to Redis, counter disabled</i>"
  17.  
  18. html = "<h3>Hello {name}!</h3>" \
  19. "<b>Hostname:</b> {hostname}<br/>" \
  20. "<b>Visits:</b> {visits}"
  21. return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
  22.  
  23. if __name__ == "__main__":
  24. app.run(host='0.0.0.0', port=80)
Add Comment
Please, Sign In to add comment