quanttp2/quanttp/__main__.py

105 lines
4.6 KiB
Python
Raw Normal View History

2020-05-21 21:05:12 +07:00
# Copyright (c) 2020 Andika Wasisto
# Modified by Tobias Raayoni Last
2020-05-21 21:05:12 +07:00
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2020-09-07 20:45:21 +07:00
2020-08-17 05:03:36 +07:00
import os
2020-05-25 12:35:08 +07:00
import threading
import json
2020-05-25 12:35:08 +07:00
2020-05-21 21:05:12 +07:00
from flask import Flask, request, Response
from flask_sockets import Sockets
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
2020-08-17 05:03:36 +07:00
from quanttp.data.qng_wrapper_linux import QngWrapperLinux
from quanttp.data.qng_wrapper_windows import QngWrapperWindows
2020-05-25 12:56:13 +07:00
2020-05-21 21:05:12 +07:00
app = Flask(__name__)
sockets = Sockets(app)
2020-08-17 05:03:36 +07:00
qng_wrapper = QngWrapperWindows() if (os.name == 'nt') else QngWrapperLinux()
2020-05-21 21:05:12 +07:00
@app.route('/api/randint32')
2020-09-07 20:45:21 +07:00
def randint32():
try:
2020-09-17 21:02:14 +02:00
length = int(request.args.get('length'))
if length < 1:
return Response('length must be greater than 0', status=400, content_type='text/plain')
int32array = []
2020-09-17 21:02:14 +02:00
for x in range(0, length):
int32array.append(qng_wrapper.randint32())
2020-09-17 21:02:14 +02:00
return Response(json.dumps({"type": "string", "format": "int32", "length":length, "data": int32array, "success": "true"}), content_type='text/plain')
except (TypeError, ValueError) as e:
return Response(json.dumps({"error": str(e), "success":"false"}), status=400, content_type='text/plain')
2020-05-21 21:05:12 +07:00
@app.route('/api/randuniform')
2020-09-07 20:45:21 +07:00
def randuniform():
try:
2020-09-17 21:02:14 +02:00
length = int(request.args.get('length'))
if length < 1:
return Response('length must be greater than 0', status=400, content_type='text/plain')
uniformarray = []
2020-09-17 21:02:14 +02:00
for x in range(0, length):
uniformarray.append(qng_wrapper.randuniform())
2020-09-17 21:02:14 +02:00
return Response(json.dumps({"type": "string", "format": "uniform", "length":length, "data": uniformarray, "success": "true"}), content_type='text/plain')
except (TypeError, ValueError) as e:
return Response(json.dumps({"error": str(e), "success":"false"}), status=400, content_type='text/plain')
2020-05-21 21:05:12 +07:00
@app.route('/api/randnormal')
2020-09-07 20:45:21 +07:00
def randnormal():
try:
2020-09-17 21:02:14 +02:00
length = int(request.args.get('length'))
if length < 1:
return Response('length must be greater than 0', status=400, content_type='text/plain')
normarray = []
2020-09-17 21:02:14 +02:00
for x in range(0, length):
normarray.append(qng_wrapper.randnormal())
2020-09-17 21:02:14 +02:00
return Response(json.dumps({"type": "string", "format": "normal", "length":length, "data": normarray, "success": "true"}), content_type='text/plain')
except (TypeError, ValueError) as e:
return Response(json.dumps({"error": str(e), "success":"false"}), status=400, content_type='text/plain')
2020-05-21 21:05:12 +07:00
@app.route('/api/randhex')
def randhex():
2020-05-21 21:05:12 +07:00
try:
length = int(request.args.get('length'))
size = int(request.args.get('size'))
2020-07-11 18:22:44 +07:00
if length < 1:
return Response('length must be greater than 0', status=400, content_type='text/plain')
if size < 1:
return Response('size must be greater than 0', status=400, content_type='text/plain')
hexarray = []
2020-09-17 21:02:14 +02:00
for x in range(0, length):
hexarray.append(qng_wrapper.randbytes(size).hex())
return Response(json.dumps({"type": "string", "format": "hex", "length":length, "size": size, "data": hexarray, "success": "true"}), content_type='text/plain')
2020-09-07 20:56:42 +07:00
except (TypeError, ValueError) as e:
return Response(json.dumps({"error": str(e), "success":"false"}), status=400, content_type='text/plain')
2020-05-21 21:05:12 +07:00
2020-09-07 20:45:21 +07:00
@app.route('/api/clear')
def clear():
qng_wrapper.clear()
return Response(status=204)
2020-05-21 21:05:12 +07:00
@app.errorhandler(Exception)
def handle_exception(e):
return Response(e.description, status=e.code, content_type='text/plain')
server = pywsgi.WSGIServer(('0.0.0.0', 62456), application=app, handler_class=WebSocketHandler)
server.serve_forever()