Created
June 13, 2016 02:57
-
-
Save tanner0101/4ea9c7ca9ba832f16a633381dd5071e3 to your computer and use it in GitHub Desktop.
Flask Benchmark
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
import sys | |
import flask | |
import random | |
import sqlite3 | |
import logging | |
import socket | |
logging.basicConfig(level=logging.WARNING, format='%(levelname)s: %(message)s') | |
app = flask.Flask(__name__) | |
application = app | |
db = sqlite3.connect('./test.sqlite') | |
conn = db.cursor() | |
conn.row_factory = sqlite3.Row | |
@app.route("/plaintext") | |
def plaintext(): | |
return "Hello, world!" | |
@app.route("/json") | |
def json(): | |
return flask.jsonify(**{ | |
"array": [1, 2, 3], | |
"dict": {"one": 1, "two": 2, "three": 3}, | |
"int": 42, | |
"string": "test", | |
"double": 3.14, | |
"null": None | |
}) | |
@app.route("/sqlite-fetch") | |
def sqlite_fetch(): | |
id = random.randint(1, 3) | |
r = conn.execute("select * from users where id = ?", (id,)).fetchone() | |
if r is not None: | |
d = dict(zip(r.keys(), r)) | |
return flask.jsonify(d) | |
else: | |
flask.abort(404) | |
if __name__ == "__main__": | |
port = 8137 | |
print 'Listening on port %s' % port | |
while True: | |
try: | |
app.run(port=port, host="107.170.131.198") | |
sys.exit(0) | |
except socket.error as e: | |
logging.warn("socket error: %s" % e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment