Guest User

Untitled

a guest
Aug 15th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # flakey_server.py
  5. #
  6. # Created by Lars Yencken on 2011-10-12.
  7. #
  8.  
  9. """
  10. A web server which fails randomly and occasionally.
  11. """
  12.  
  13. import sys
  14. import random
  15.  
  16. import flask
  17.  
  18. app = flask.Flask(__name__)
  19.  
  20. FAILURE_CHANCE = 0.0
  21.  
  22. @app.route('/')
  23. def index():
  24. if random.random() < FAILURE_CHANCE:
  25. flask.abort(500)
  26. else:
  27. return 'OK'
  28.  
  29. def main():
  30. global FAILURE_CHANCE
  31. args = sys.argv[1:]
  32. if len(args) != 2:
  33. print >> sys.stderr, \
  34. """Usage: sometimes_fail.py <rate> <port>
  35.  
  36. Serve error responses with probability <rate> on port <port>."""
  37. sys.exit(1)
  38.  
  39. rate, port = args
  40. port = int(port)
  41. rate = float(rate)
  42. FAILURE_CHANCE = rate
  43. app.run(host='0.0.0.0', port=port)
  44.  
  45. rate = float(args[0])
  46.  
  47. if __name__ == '__main__':
  48. main()
Add Comment
Please, Sign In to add comment