Webserver

Overview

data desktop

Python Serer

from flask import Flask, request, jsonify import treasury as ty import yahoo as yh # Initialize the Flask application app = Flask(__name__) # This function runs automatically for every single route # only allows local connections. @app.before_request def execute_before_every_route(): #if you want to allow all connection, uncomment out the next line #return None if request.remote_addr in ['127.0.0.1', '::1']: # Returning None allows Flask to proceed to the standard route return None # Returning a response immediately terminates further route processing return jsonify({"error": "Unauthorized"}), 401 # Define the route for the home page @app.route("/") def hello_world(): if request.remote_addr in ['127.0.0.1', '::1']: return "Hello Localhost!" return "Hello, World!" @app.route('/treasury/') def treasury(year): # 'year' is a string here data = ty.get(year) return jsonify(data) @app.route('/yahoo/') def yahoo(ticker): # 'year' is a string here data = yh.history(ticker) return jsonify(data) # Start the local development server if __name__ == "__main__": app.run(debug=True, port=8080)

Node Js Proxy

The above python server uses Flask, which is not recommended for high traffic production environments. A simple way to use the server is to place a proxy in front of it, that only rountes a certain number of concurrent calls to the python server.

In this example we build a simple Node Js Express webserver that forwards only 3 concurrent requests to the python server running on the local machine.
const https = require('https'); const httpsAgent = new https.Agent({ rejectUnauthorized: false }); const axios = require('axios').default; let tasks = []; let taskMap = {}; const concurrent = 3; function any(){ return new Promise(function(resolve, reject){ let resolved = false; for(let promise of tasks){ promise.then(function(){ if(resolved === false){ resolved = true; resolve(); } }).catch(function(){ if(resolved === false){ resolved = true; resolve(); } }) } }); } function removePromise(promise){ let index = -1; for(let i=0;i<ltasks.length;i++){ if(tasks[i] === promise){ index = i; } if(index !== -1){ tasks.splice(index, 1); if(waiting.length>0){ let next = waiting.shift(); next(); } } } } let waiting = []; async function post(url, data){ return new Promise(async function(resolve, reject){ async function runOne(){ let promise = post2(url, data); try{ tasks.push(promise); let result = await promise; removePromise(promise); resolve(result); } catch(err){ removePromise(promise); reject(err); } } if(tasks.length<concurrent) runOne(); else { waiting.push(async function(){ runOne(); }); } }); } async function get(url){ return new Promise(function(resolve, reject){ async function runOne(){ let promise = get2(url); try{ tasks.push(promise); let result = await promise; removePromise(promise); resolve(result); } catch(err){ removePromise(promise); reject(err); } } if(tasks.length<concurrent) runOne(); else { waiting.push(async function(){ runOne(); }); } }); } async function post2(url, data){ const result = await axios.post(url, data, //this ignores invalid certificates { httpsAgent: httpsAgent } ); return result.data; } async function get2(url){ const result = await axios.get(url, //this ignores invalid certificates { httpsAgent: httpsAgent } ); return result.data; } module.exports = { get,post };