Eclectic Media Git auto_bandwidth_test / master app.py
master

Tree @master (Download .tar.gz)

app.py @masterraw · history · blame

import os
import jwt
import time
import sys
from json import dumps
from pprint import pprint
from flask import Flask, jsonify, request, abort

from lib.authorization import token_required, authorizations

app = Flask(__name__)
app.secret_key = os.urandom(24)


def generate_token():
    token = jwt.encode({'user': 'tester'}, app.config['SECRET_KEY'])
    with open('mykey', 'w+') as keyfile:
        keyfile.write(token.decode('UTF-8'))


@app.route('/', methods=['PUT'])
@token_required
def put_data():
    json_data = request.get_json()
    submission_instance = [request.remote_addr, time.asctime()]

    if not list(json_data).count('data'):
        abort(400, "missing 'data' json key!")

    if not json_data['data'][0].count('[  3]'):
        abort(400, "bad data!")

    submission_instance.append(json_data['data'])

    with open('bandwidth_test.log', 'a+') as f:
        f.write(dumps(submission_instance))

    return 'submission ok'


if __name__ == '__main__':
    if sys.argv.count('--generate'):
        generate_token()
        exit(0)

    app.run()