Example of using circuits and it's Web Components to build a simple web application that handles some basic form data.
""" def index(self): return self.html def submit(self, firstName, lastName): return "Hello %s %s" % (firstName, lastName) (Server(8000) + Root()).run( How Do I: Upload a File ----------------------- You can easily handle File Uploads as well using the same techniques as above. Basically the "name" you give your tag of type="file" will get passed as the Keyword Argument to your Request Handler. It has the following two attributes:: .filename - The name of the uploaded file. .value - The contents of the uploaded file. Here's the code! .. code-block:: python :linenos: #!/usr/bin/env python from circuits.web import Server, Controller UPLOAD_FORM = """
Filename: %s
Description: %s
File Contents:
%s""" class Root(Controller): def index(self, file=None, desc=""): if file is None: return UPLOAD_FORM else: filename = file.filename return UPLOADED_FILE % (file.filename, desc, file.value) (Server(8000) + Root()).run() circuits.web automatically handles form and file uploads and gives you access to the uploaded file via arguments to the request handler after they've been processed by the dispatcher. How Do I: Integrate with WSGI Applications ------------------------------------------ Integrating with other WSGI Applications is quite easy to do. Simply add in an instance of the :class:`~circuits.web.wsgi.Gateway` component into your circuits.web application. Example: .. code-block:: python :linenos: #!/usr/bin/env python from circuits.web.wsgi import Gateway from circuits.web import Controller, Server def foo(environ, start_response): start_response("200 OK", [("Content-Type", "text/plain")]) return ["Foo!"] class Root(Controller): """App Rot""" def index(self): return "Hello World!" app = Server(("0.0.0.0", 10000)) Root().register(app) Gateway({"/foo": foo}).register(app) app.run() The ``apps`` argument of the :class:`~circuits.web.wsgi.Gateway` component takes a key/value pair of ``path -> callable`` (*a Python dictionary*) that maps each URI to a given WSGI callable. How Do I: Deploy with Apache and mod_wsgi ----------------------------------------- Here's how to deploy your new Circuits powered Web Application on Apache using mod_wsgi. Let's say you have a Web Hosting account with some provider. - Your Username is: "joblogs" - Your URL is: http://example.com/~joeblogs/ - Your Docroot is: /home/joeblogs/www/ Configuring Apache .................. The first step is to add in the following .htaccess file to tell Apache hat we want any and all requests to http://example.com/~joeblogs/ to be served up by our circuits.web application. Created the .htaccess file in your **Docroot**:: ReWriteEngine On ReWriteCond %{REQUEST_FILENAME} !-f ReWriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /~joeblogs/index.wsgi/$1 [QSA,PT,L] Running your Application with Apache/mod_wsgi ............................................. The get your Web Application working and deployed on Apache using mod_wsgi, you need to make a few changes to your code. Based on our Basic Hello World example earlier, we modify it to the following: .. code-block:: python :linenos: #!/usr/bin/env python from circuits.web import Controller from circuits.web.wsgi import Application class Root(Controller): def index(self): return "Hello World!" application = Application() + Root() That's it! To run this, save it as index.wsgi and place it in your Web Root (public-html or www directory) as per the above guidelines and point your favorite Web Browser to: http://example.com/~joeblogs/ .. note:: It is recommended that you actually use a reverse proxy setup for deploying circuits.web web application so that you don't loose the advantages and functionality of using an event-driven component architecture in your web apps. In **production** you should use a load balance and reverse proxy combination for best performance.