simpleweb is a dummy web library for python. It allows developer to specify a function, pass it to simpleweb.run() and simpleweb will execute it as a FastCGI program. It use the flup package for the FastCGI functionality.
simpleweb.py
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 '<html><h1>my name is kamal</h1</html>' 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
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 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 '<html>' print '<h1>hello %s</h1>' % name input = web.input() print '<p>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.
Method and approach for dispatching is still debateable. The goal is to find the simplest possible solution.
Q: Why don't just use web.py ?
A: I like the approach taken by web.py but the license was so restrictive. Clause 2(d) of the license state that:-
If the Program as you received it is intended to interact with users through a computer network and if, in the version you received, any user interacting with the Program was given the opportunity to request transmission to that user of the Program's complete source code, you must not remove that facility from your modified version of the Program or work based on the Program, and must offer an equivalent opportunity for all users interacting with your Program through a computer network to request immediate transmission by HTTP of the complete source code of your modified version or other derivative work.
Q: Just use php, dammit !!
A: I love python
Q: What about template ?
A: I hate template but you can use any template engine such as Cheetah. That's the purpose of simpleweb. Use anything you want.
Q: Another framework … ? This is suck.
A: I know.
http://www.saddi.com/software/flup/
http://cleverdevil.org/computing/24/python-fastcgi-wsgi-and-lighttpd
http://pythonpaste.org/do-it-yourself-framework.html
http://www.python.org/peps/pep-0333.html
Notes
For anyone interested in developing simpleweb, they are highly discouraged to look into web.py code. This is to ensure clean code without any relation to web.py code.