Created
October 11, 2011 23:47
-
-
Save larsyencken/1279819 to your computer and use it in GitHub Desktop.
A web server which fails according to known probability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# flakey_server.py | |
# | |
# Created by Lars Yencken on 2011-10-12. | |
# | |
""" | |
A web server which fails randomly and occasionally. | |
""" | |
import sys | |
import random | |
import flask | |
app = flask.Flask(__name__) | |
FAILURE_CHANCE = 0.0 | |
@app.route('/') | |
def index(): | |
if random.random() < FAILURE_CHANCE: | |
flask.abort(500) | |
else: | |
return 'OK' | |
def main(): | |
global FAILURE_CHANCE | |
args = sys.argv[1:] | |
if len(args) != 2: | |
print >> sys.stderr, \ | |
"""Usage: sometimes_fail.py <rate> <port> | |
Serve error responses with probability <rate> on port <port>.""" | |
sys.exit(1) | |
rate, port = args | |
port = int(port) | |
rate = float(rate) | |
FAILURE_CHANCE = rate | |
app.run(host='0.0.0.0', port=port) | |
rate = float(args[0]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment