Created
May 6, 2012 03:28
-
-
Save ryancutter/2608632 to your computer and use it in GitHub Desktop.
Simple Python/Tornado web server tailored for hosting on OpenShift (https://openshift.redhat.com)
This file contains 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
#!/usr/bin/python | |
import os | |
virtenv = os.environ['APPDIR'] + '/virtenv/' | |
os.environ['PYTHON_EGG_CACHE'] = os.path.join(virtenv, 'lib/python2.6/site-packages') | |
virtualenv = os.path.join(virtenv, 'bin/activate_this.py') | |
try: | |
execfile(virtualenv, dict(__file__=virtualenv)) | |
except IOError: | |
pass | |
# | |
# IMPORTANT: Put any additional includes below this line. If placed above this | |
# line, it's possible required libraries won't be in your searchable path | |
# | |
# this file lives at <appname>/wsgi/application | |
import tornado.wsgi | |
import tornado.web | |
from json import loads | |
from time import time, gmtime, strftime | |
from uuid import uuid4 | |
# serve up simple HTML form | |
class MainHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write("<html><head><title>Test Form</title></head><body>") | |
self.write("<form name='input' action='/jobs/' method='post'>") | |
self.write("<input type='hidden' name='newjob' value='yes'>") | |
self.write("Term:<input type='text' name='value'/><br>") | |
self.write("<input type='submit' value='Go!'/></form>") | |
self.write("</body></html>") | |
# /jobs | |
class JobsHandler(tornado.web.RequestHandler): | |
# serve table of stuff ("jobs" in this case) | |
def servetable(self): | |
self.write("<html><head><title>Table</title></head><body>") | |
self.write("Jobs:<p>") | |
jobsout = open("/path/to/file", "r") | |
completed = {} | |
for row in jobsout.readlines(): | |
vals = row.split(",") | |
self.write("<tr><td><a href='/job/" + vals[0] + "'>" + vals[0] + "</a></td><td>" + vals[1] + "</td></tr>") | |
jobsout.close() | |
self.write("</table></body></html>") | |
# print table, that's it | |
def get(self): | |
self.servetable() | |
# process form | |
def post(self): | |
if self.get_argument("newjob", None): | |
v = self.get_argument("value") | |
f = open("/path/to/file", "a") | |
t = int(round(time() * 1000)) | |
f.write(str(uuid4()) + "," + v + "," + str(t) + "\n") | |
f.close() | |
self.servetable() | |
# /job/ | |
class JobHandler(tornado.web.RequestHandler): | |
def get(self, val): | |
self.write("<html><head><title>Job Details</title></head><body>") | |
self.write("Log for job " + str(val) + ":<p>") | |
# open jobfile (not created by this app) | |
f = open('/path/to/jobfile' + val + '.job') | |
for r in f: | |
self.write(r + "<br>") | |
f.close() | |
self.write("</body></html>") | |
# here's where the server decides where to route the request | |
application = tornado.wsgi.WSGIApplication([ | |
(r"/", MainHandler), # this serves up a form | |
(r"/([0-9]+)", MainHandler), | |
(r"/jobs/", JobsHandler), # this might necessitate a form being processed | |
(r"/job/(.*?)", JobHandler) | |
]) | |
if __name__ == '__main__': | |
from wsgiref.simple_server import make_server | |
httpd = make_server('localhost', 8051, application) | |
httpd.handle_request() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment