Skip to content

Instantly share code, notes, and snippets.

@flengyel
Forked from joshourisman/gist:970746
Created December 7, 2011 22:16
Show Gist options
  • Save flengyel/1444972 to your computer and use it in GitHub Desktop.
Save flengyel/1444972 to your computer and use it in GitHub Desktop.
Basic Flask REST server
class ReverseProxied(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
script_name = environ.get('HTTP_X_SCRIPT_NAME', None)
if script_name is not None:
environ['SCRIPT_NAME'] = script_name
path_info = environ['PATH_INFO']
if path_info.startswith(script_name):
path_info = path_info[len(script_name):]
environ['PATH_INFO'] = path_info
scheme = environ.get('HTTP_X_SCHEME', None)
if scheme is not None:
environ['wsgi_url_scheme'] = scheme
return self.app(environ, start_response)
from flask import Flask
from subprocess import Popen, PIPE
from reverseproxied import ReverseProxied
app = Flask(__name__)
app.wsgi_app = ReverseProxied(app.wsgi_app)
@app.route('/')
def list_functions():
return 'netAccumulate'
@app.route('/ls')
def ls_app():
return Popen('ls', stdout=PIPE,shell=True).stdout.read()
@app.route('/ls/<path:dir>')
def ls_argh(dir):
p = Popen('ls'+' '+dir, stdout=PIPE,stderr=PIPE,shell=True)
print p.wait()
return p.stdout.read() + p.stderr.read()
@app.route('/netAccumulate')
def netacc():
p=Popen('/usr/local/share/ghaas/bin/netAccumulate --help',
stdout=PIPE,stderr=PIPE,shell=True)
print p.wait()
return p.stdout.read() + p.stderr.read()
if __name__ == '__main__':
app.run(host='0.0.0.0')
#!/bin/bash
# activate the virtualenv
# from outside
#. $HOME/REST/bin/activate
# detach the REST server
# save the pid to file descriptor 3
( python rgis.py & echo $! >&3 ) 3>pid
# leave sufficient time for the REST server to launch
sleep 2
#echo $(<pid)
# run the tests
curl http://localhost:5000/
curl http://localhost:5000/ls
curl http://localhost:5000/ls/bin
curl http://localhost:5000/netacc
curl http://lily.ccny.cuny.edu/rgis/netacc
# kill the server process
kill $(<pid)
@flengyel
Copy link
Author

flengyel commented Dec 7, 2011

The ReverseProxied class seems to come from http://flask.pocoo.org/snippets/35/ with some modifications. It does work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment