Your application is probably going to have to deal with user input somewhere along the line, and, fortunately, CherryPy makes user input easy to work with. User input is passed in the form of arguments to methods, like this: import cherrypy class InputExample: def index ( self ): cherrypy.root = InputExample() In the above example, we set the default value of email to None, but, of course, you can set it to whatever value you would like. As you can see above, handling user input is not a complex issue. Simply make your function accept user input as arguments, and then manipulate or access those variables as needed. User Sessions Sessions are used to store user data in between pages. In order to access them in CherryPy, we must modify our configuration file a bit to enable sessions: [global] server.socketPort = 8080 Now, we are able to access sessions via the cherrypy.session object. Here's a simple session example: import cherrypy class SessionExample: def index ( self ): cherrypy.config.update ( file = 'development.conf' ) When the user first visits the page, he or she sees a form with a color field. When the user selects a value, that value is stored inside of a session, and a link back to the first page is displayed. This time, however, the user will see the name of the chosen color displayed in the chosen color. The form will also be visible so that the chosen color may be changed. Sessions work fairly simply. Each session name is stored as a key, with the session's value stored as the value of that key. We can check for the existence of a particular key by using the has_key method of cherrypy.session. Again, however, sessionFilter.on must be set to True in order for sessions to be enabled in your CherryPy application. Conclusion Though there are quite a few different web development platforms available for Python, CherryPy stands out because it makes heavy use of object orientation. Each page is a method contained in an object, and the elements in a request match the names of objects and methods. Moreover, each CherryPy script is actually an independent server. Tasks such as accepting user input and manipulating user sessions are especially easy with CherryPy, and making CherryPy work behind Apache is an easy feat. CherryPy allows for simple, object oriented web development in a simple, object oriented language.
blog comments powered by Disqus |
|
|
|
|
|
|
|