Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
|Failed to load latest commit information.|
py3web is a web framework for Python 3.2+, designed to make web programming simple, intuitive, and rapid. There is no "black magic" that happens behind the scenes -- as the Zen says, "explicit is better than implicit." The framework makes liberal use of lazy initialization, so you don't pay for what you don't use. Here's a quick and dirty demo of the framework's basic features. To run it, point your favorite CGI or WSGI server at a module containing this code: #!/usr/bin/python from py3web import cgi, html, wsgi def application(environ, start_response): # WSGI entry point: We launch the framework, passing it the WSGI environment # and our routing function. py3web will call the routing function, and then # either call the first matching handler, or emit a 404 if no match is found. return wsgi.run(environ, start_response, route=do_routes) def do_routes(router): # Routes are defined using regexes, which are automatically surrounded by ^$. router.redirect('', 'ask/', 301) router.route('ask/', ask) router.route('hello/', hello, method='POST') def ask(web): # The client object is canonically passed around using the name 'web'. Just about all # interaction with the framework happens through either 'web.request' or 'web.response'. web.response.write( '''<form method=post action="/hello/"> <p><label>Enter your name: <input name=name></label> <p><input type=submit> </form>''') def hello(web): try: name = web.request.post_vars['name'].string() except Exception: web.response.write('Forget something?') else: web.response.write('Greetings, <em>' + html.escape(name) + '</em>!') if __name__ == '__main__': cgi.run(route=do_routes) For development purposes, you can use Python's batteries-included reference WSGI implementation. Don't ever use it in a production environment, though! if __name__ == '__main__': from wsgiref import simple_server httpd = simple_server.make_server('', 80, application) httpd.serve_forever() If you're looking for a production-ready pure Python 3 WSGI web server, I recommend taking a look at the one included with CherryPy. It's probably about as fast as you can get without resorting to C code, and it can be used independently from the rest of CherryPy: if __name__ == '__main__': from cherrypy import wsgiserver httpd = wsgiserver.CherryPyWSGIServer(('127.0.0.1', 80), application) try: httpd.start() finally: httpd.stop()