import sys
import os
from StringIO import StringIO
def run(func):
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
oldstdout = sys.stdout
try:
sys.stdout = out = StringIO()
func()
return [str(out.getvalue())]
finally:
sys.stdout = oldstdout
from flup.server.fcgi import WSGIServer
WSGIServer(app, multiplexed=True).run()
test.py
#!/usr/bin/env python
import simpleweb as web
def output():
print 'my name is kamal
'
if __name__ == '__main__':
web.run(output)
lighttpd.conf
server.modules = ( "mod_rewrite",
"mod_access",
"mod_fastcgi",
"mod_accesslog")
server.port = 8080
server.document-root = "/home/kamal/web/junk"
$HTTP["host"] == "kxblog.laptop.int" {
fastcgi.debug = 1
server.errorlog = "/home/kamal/lighttpd/lighttpd-error.log"
accesslog.filename = "/home/kamal/lighttpd/access_log"
server.document-root = "/home/kamal/web/junk"
#server.error-handler-404 = "/test.py"
fastcgi.server = ("/test.py" =>
(("socket" => "/tmp/fcgi.sock",
"bin-path" => "/home/kamal/web/junk/test.py",
"max-procs" => 1
))
)
}
The application can now be access at http://kxblog.laptop.int:8080/test.py
===== Idea =====
Create a lightweight web framework that provides simple dispatching to user defined object. The framework also will provide easy access to request object such user input through query string, form or cookies and response object which display the output back to browser. It's heavily influenced by [[http://webpy.org/|web.py]]. The following snippet would show possible usage of simpleweb.
import simpleweb as web
urls = (
'/node/(*)', 'Node',
)
class Node:
def GET(self, name):
print ''
print 'hello %s
' % name
input = web.input()
print 'user input are %s' % list(input)
if __name__ == '__main__':
web.run(urls)
User only need to define an object that implement GET or POST method. This would allow the creation of web application that conform to the REST paradigm. FIXME
Method and approach for dispatching is still debateable. The goal is to find the simplest possible solution.
===== Objectives=====
* Simple dispatching so user could easily exposed their object to the web
* It should be easy to deploy. Currently the intention only to make it work as FastCGI under Lighttpd.
* Distinguish between GET and POST method.
* For anything else, just use the Python Standard Library or other third party modules.
===== FAQ =====
Q: Why don't just use web.py ? \\
A: I like the approach taken by web.py but the [[http://www.affero.org/oagpl.html|license]] was so restrictive. Clause 2(d) of the license state that:-