Skip to content

Instantly share code, notes, and snippets.

@hartym
Created August 18, 2011 05:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hartym/1153363 to your computer and use it in GitHub Desktop.
Save hartym/1153363 to your computer and use it in GitHub Desktop.
the_early_morning_framework.py
"""
Welcome to "what better thing to do than coding a web app while drinking my
morning coffee"-framework.
It show how easy it is to code a standalone/state-of-the-art mini web application
using python, and some amazing libraries available around.
Disclaimer
----------
This is only for educational purposes, and should be used as it. It's NOT secure,
and enables to run arbitrary python commands in the context of the application
using the ajax debugger middleware.
Dependencies
------------
* webob : http request and response objects.
* jinja2 : a templating engine
* weberror : a wsgi middleware that enpowers your app with an ajax debugger.
* gevent : an amazing networking library.
Bootstrap on debian-like systems
--------------------------------
$ sudo apt-get install build-essential libevent-dev python python-dev python-setuptools
$ easy_install webob jinja2 weberror gevent
$ python the_early_morning_framework.py
Then open your web browser.
* http://localhost:2222/ : hello world
* http://localhost:2222/error : interactive debugger on exception
* http://localhost:2222/jinja?value=hello : the jinja example
* http://localhost:2222/whatever : 404
You should consider installing this in a separate virtualenv, but this is not
the topic.
"""
from webob import ( Request as HttpRequest,
Response as HttpResponse )
templates = {'jinja': '''<html>
<head><title>Welcome here</title></head>
<body>
<h1>Welcome</h1>
The value is {{ value|default('not set') }}.
</body>
</html>'''}
###############################################################################
# Controllers
def index_controller(request):
return HttpResponse('Hello dude.')
def jinja_controller(request):
from jinja2 import Environment, DictLoader
loader = DictLoader(templates)
environment = Environment(loader=loader)
template = environment.get_template('jinja')
return HttpResponse(template.render({'value': request.GET.get('value', 'not available')}))
def error_controller(request):
raise Exception, 'This is an exception throwing controller'
def not_found_controller(request):
return HttpResponse('Not found.', '404')
###############################################################################
# Controller resolver
def resolve_controller(request):
if request.path_info == '/':
return index_controller
elif request.path_info == '/jinja':
return jinja_controller
elif request.path_info == '/error':
return error_controller
return not_found_controller
###############################################################################
# Http app, transforms a webob.Request into a webob.Response
def http_app(request):
controller = resolve_controller(request)
response = controller(request)
return response
###############################################################################
# WSGI app, adapts our http app to the WSGI protocol.
def wsgi_app(environ, start_response):
request = HttpRequest(environ=environ)
response = http_app(request)
start_response(response.status, response.headerlist)
return response.body
###############################################################################
# Fancy ajax debugger in web browser
from weberror.evalexception import EvalException as AjaxDebuggerMiddleware
__DEBUG__ = True
if __DEBUG__:
wsgi_app = AjaxDebuggerMiddleware(wsgi_app)
###############################################################################
# Standalone high performance WSGI server
if __name__ == '__main__':
from gevent import wsgi
wsgi.WSGIServer(('', 2222), wsgi_app, spawn=None).serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment