CherryPy – Vocabulary ”; Previous Next There are a few important keywords which need to be defined in order to understand the working of CherryPy. The keywords and the definitions are as follows − S.No Keyword & Definition 1. Web Server It is an interface dealing with the HTTP protocol. Its goal is to transform the HTTP requests to the application server so that they get the responses. 2. Application It is a piece of software which gathers information. 3. Application server It is the component holding one or more applications 4. Web application server It is the combination of web server and application server. Example The following example shows a sample code of CherryPy − import cherrypy class demoExample: def index(self): return “Hello World!!!” index.exposed = True cherrypy.quickstart(demoExample()) Let us now understand how the code works − The package named CherryPy is always imported in the specified class to ensure proper functioning. In the above example, the function named index returns the parameter “Hello World!!!”. The last line starts the web server and calls the specified class (here, demoExample) and returns the value mentioned in default function index. The example code returns the following output − Print Page Previous Next Advertisements ”;
Category: cherrypy
CherryPy – Web Services
CherryPy – Web Services ”; Previous Next A web service is a set of web-based components that helps in the exchange of data between the application or systems which also includes open protocols and standards. It can be published, used and found on the web. Web services are of various types like RWS (RESTfUL Web Service), WSDL, SOAP and many more. REST — Representational State Transfer A type of remote access protocol, which, transfers state from client to server which can be used to manipulate state instead of calling remote procedures. Does not define any specific encoding or structure and ways of returning useful error messages. Uses HTTP “verbs” to perform state transfer operations. The resources are uniquely identified using URL. It is not an API but instead an API transport layer. REST maintains the nomenclature of resources on a network and provides unified mechanism to perform operations on these resources. Each resource is identified by at least one identifier. If the REST infrastructure is implemented with the base of HTTP, then these identifiers are termed as Uniform Resource Identifiers (URIs). The following are the two common subsets of the URI set − Subset Full form Example URL Uniform Resource Locator http://www.gmail.com/ URN Uniform Resource Name urn:isbn:0-201-71088-9 urn:uuid:13e8cf26-2a25-11db-8693-000ae4ea7d46 Before understanding the implementation of CherryPy architecture, let’s focus on the architecture of CherryPy. CherryPy includes the following three components − cherrypy.engine − It controls process startup/teardown and event handling. cherrypy.server − It configures and controls the WSGI or HTTP server. cherrypy.tools − A toolbox of utilities that are orthogonal to processing an HTTP request. REST Interface through CherryPy RESTful web service implements each section of CherryPy architecture with the help of the following − Authentication Authorization Structure Encapsulation Error Handling Authentication Authentication helps in validating the users with whom we are interacting. CherryPy includes tools to handle each authentication method. def authenticate(): if not hasattr(cherrypy.request, ”user”) or cherrypy.request.user is None: # < Do stuff to look up your users > cherrypy.request.authorized = False # This only authenticates. Authz must be handled separately. cherrypy.request.unauthorized_reasons = [] cherrypy.request.authorization_queries = [] cherrypy.tools.authenticate = cherrypy.Tool(”before_handler”, authenticate, priority=10) The above function authenticate() will help to validate the existence of the clients or users. The built-in tools help to complete the process in a systematic way. Authorization Authorization helps in maintaining the sanity of the process via URI. The process also helps in morphing objects by user token leads. def authorize_all(): cherrypy.request.authorized = ”authorize_all” cherrypy.tools.authorize_all = cherrypy.Tool(”before_handler”, authorize_all, priority=11) def is_authorized(): if not cherrypy.request.authorized: raise cherrypy.HTTPError(“403 Forbidden”, ”,”.join(cherrypy.request.unauthorized_reasons)) cherrypy.tools.is_authorized = cherrypy.Tool(”before_handler”, is_authorized, priority = 49) cherrypy.config.update({ ”tools.is_authorized.on”: True, ”tools.authorize_all.on”: True }) The built-in tools of authorization help in handling the routines in a systematic way, as mentioned in the previous example. Structure Maintaining a structure of API helps in reducing the work load of mapping the URI of application. It is always necessary to keep API discoverable and clean. The basic structure of API for CherryPy framework should have the following − Accounts and User Autoresponder Contact File Folder List and field Message and Batch Encapsulation Encapsulation helps in creating API which is lightweight, human readable and accessible to various clients. The list of items along with Creation, Retrieval, Update and Deletion requires encapsulation of API. Error Handling This process manages errors, if any, if API fails to execute at the particular instinct. For example, 400 is for Bad Request and 403 is for unauthorized request. Example Consider the following as an example for database, validation, or application errors. import cherrypy import json def error_page_default(status, message, traceback, version): ret = { ”status”: status, ”version”: version, ”message”: [message], ”traceback”: traceback } return json.dumps(ret) class Root: _cp_config = {”error_page.default”: error_page_default} @cherrypy.expose def index(self): raise cherrypy.HTTPError(500, “Internal Sever Error”) cherrypy.quickstart(Root()) The above code will produce the following output − Management of API (Application Programming Interface) is easy through CherryPy because of the built-in access tools. HTTP Methods The list of HTTP methods which operate on the resources are as follows − S.No HTTP Method & Operation 1. HEAD Retrieves the resource metadata. 2. GET Retrieves the resource metadata and content. 3. POST Requests the server to create a new resource using the data enclosed in the request body. 4. PUT Requests the server to replace an existing resource with the one enclosed in the request body. 5. DELETE Requests the server to remove the resource identified by that URI. 6. OPTIONS Requests the server to return details about capabilities either globally or specifically towards a resource. Atom Publishing Protocol (APP) APP has arisen from the Atom community as an application-level protocol on top of HTTP to allow the publishing and editing of web resources. The unit of messages between an APP server and a client is based on the Atom XML-document format. The Atom Publishing Protocol defines a set of operations between an APP service and a user-agent using HTTP and its mechanisms and the Atom XML-document format as the unit of messages. APP first defines a service document, which provides the user agent with the URI of the different collections served by the APP service. Example Let us take an example to demonstrate how APP works − <?xml version = “1.0” encoding = “UTF-8”?> <service xmlns = “http://purl.org/atom/app#” xmlns:atom = “http://www.w3.org/2005/Atom”> <workspace> <collection href = “http://host/service/atompub/album/”> <atom:title> Albums</atom:title> <categories fixed = “yes”> <atom:category term = “friends” /> </categories> </collection> <collection href = “http://host/service/atompub/film/”> <atom:title>Films</atom:title> <accept>image/png,image/jpeg</accept> </collection> </workspace> </service> APP specifies how to perform the basic CRUD operations against a member of a collection or the collection itself by using HTTP methods as described in the following table − Operation HTTP Method Status Code Content Retrieve GET 200 An Atom entry representing the resource Create POST 201 The URI of the newly created resource via the Location and Content-Location headers Update PUT 200 An Atom entry representing the resource Delete DELETE 200 None Print Page Previous Next Advertisements ”;
CherryPy – Use Of Ajax
CherryPy – Use Of Ajax ”; Previous Next Till the year 2005, the pattern followed in all web applications was to manage one HTTP request per page. The navigation of one page to another page required loading the complete page. This would reduce the performance at a greater level. Thus, there was a rise in rich client applications which used to embed AJAX, XML, and JSON with them. AJAX Asynchronous JavaScript and XML (AJAX) is a technique to create fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data behind the scenes with the server. This means that it is possible to update parts of a web page, without reloading the whole page. Google Maps, Gmail, YouTube, and Facebook are a few examples of AJAX applications. Ajax is based on the idea of sending HTTP requests using JavaScript; more specifically AJAX relies on the XMLHttpRequest object and its API to perform those operations. JSON JSON is a way to carry serialized JavaScript objects in such a way that JavaScript application can evaluate them and transform them into JavaScript objects which can be manipulated later. For instance, when the user requests the server for an album object formatted with the JSON format, the server would return the output as following − {”description”: ”This is a simple demo album for you to test”, ”author”: ‘xyz’} Now the data is a JavaScript associative array and the description field can be accessed via − data [”description”]; Applying AJAX to the Application Consider the application which includes a folder named “media” with index.html and Jquery plugin, and a file with AJAX implementation. Let us consider the name of the file as “ajax_app.py” ajax_app.py import cherrypy import webbrowser import os import simplejson import sys MEDIA_DIR = os.path.join(os.path.abspath(“.”), u”media”) class AjaxApp(object): @cherrypy.expose def index(self): return open(os.path.join(MEDIA_DIR, u”index.html”)) @cherrypy.expose def submit(self, name): cherrypy.response.headers[”Content-Type”] = ”application/json” return simplejson.dumps(dict(title=”Hello, %s” % name)) config = {”/media”: {”tools.staticdir.on”: True, ”tools.staticdir.dir”: MEDIA_DIR,} } def open_page(): webbrowser.open(“http://127.0.0.1:8080/”) cherrypy.engine.subscribe(”start”, open_page) cherrypy.tree.mount(AjaxApp(), ”/”, config=config) cherrypy.engine.start() The class “AjaxApp” redirects to the web page of “index.html”, which is included in the media folder. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” ” http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”> <html xmlns = “http://www.w3.org/1999/xhtml” lang = “en” xml:lang = “en”> <head> <title>AJAX with jQuery and cherrypy</title> <meta http-equiv = ” Content-Type” content = ” text/html; charset = utf-8″ /> <script type = ” text/javascript” src = ” /media/jquery-1.4.2.min.js”></script> <script type = ” text/javascript”> $(function() { // When the testform is submitted… $(“#formtest”).submit(function() { // post the form values via AJAX… $.post(”/submit”, {name: $(“#name”).val()}, function(data) { // and set the title with the result $(“#title”).html(data[”title”]) ; }); return false ; }); }); </script> </head> <body> <h1 id = “title”>What”s your name?</h1> <form id = ” formtest” action = ” #” method = ” post”> <p> <label for = ” name”>Name:</label> <input type = ” text” id = “name” /> <br /> <input type = ” submit” value = ” Set” /> </p> </form> </body> </html> The function for AJAX is included within <script> tags. Output The above code will produce the following output − Once the value is submitted by the user, AJAX functionality is implemented and the screen is redirected to the form as shown below − Print Page Previous Next Advertisements ”;
CherryPy – A Working Application ”; Previous Next Full stack applications provide a facility to create a new application via some command or execution of the file. Consider the Python applications like web2py framework; the entire project/application is created in terms of MVC framework. Likewise, CherryPy allows the user to set up and configure the layout of the code as per their requirements. In this chapter, we will learn in detail how to create CherryPy application and execute it. File System The file system of the application is shown in the following screenshot − Here is a brief description of the various files that we have in the file system − config.py − Every application needs a configuration file and a way to load it. This functionality can be defined in config.py. controllers.py − MVC is a popular design pattern followed by the users. The controllers.py is where all the objects are implemented that will be mounted on the cherrypy.tree. models.py − This file interacts with the database directly for some services or for storing persistent data. server.py − This file interacts with production ready web server that works properly with load balancing proxy. Static − It includes all the CSS and image files. Views − It includes all the template files for a given application. Example Let us learn in detail the steps to create a CherryPy application. Step 1 − Create an application that should contain the application. Step 2 − Inside the directory, create a python package corresponding to the project. Create gedit directory and include _init_.py file within the same. Step 3 − Inside the package, include controllers.py file with the following content − #!/usr/bin/env python import cherrypy class Root(object): def __init__(self, data): self.data = data @cherrypy.expose def index(self): return ”Hi! Welcome to your application” def main(filename): data = {} # will be replaced with proper functionality later # configuration file cherrypy.config.update({ ”tools.encode.on”: True, ”tools.encode.encoding”: ”utf-8”, ”tools.decode.on”: True, ”tools.trailing_slash.on”: True, ”tools.staticdir.root”: os.path.abspath(os.path.dirname(__file__)), }) cherrypy.quickstart(Root(data), ”/”, { ”/media”: { ”tools.staticdir.on”: True, ”tools.staticdir.dir”: ”static” } }) if __name__ == ”__main__”: main(sys.argv[1]) Step 4 − Consider an application where the user inputs the value through a form. Let’s include two forms — index.html and submit.html in the application. Step 5 − In the above code for controllers, we have index(), which is a default function and loads first if a particular controller is called. Step 6 − The implementation of the index() method can be changed in the following way − @cherrypy.expose def index(self): tmpl = loader.load(”index.html”) return tmpl.generate(title=”Sample”).render(”html”, doctype=”html”) Step 7 − This will load index.html on starting the given application and direct it to the given output stream. The index.html file is as follows − index.html <!DOCTYPE html > <html> <head> <title>Sample</title> </head> <body class = “index”> <div id = “header”> <h1>Sample Application</h1> </div> <p>Welcome!</p> <div id = “footer”> <hr> </div> </body> </html> Step 8 − It is important to add a method to the Root class in controller.py if you want to create a form which accepts values such as names and titles. @cherrypy.expose def submit(self, cancel = False, **value): if cherrypy.request.method == ”POST”: if cancel: raise cherrypy.HTTPRedirect(”/”) # to cancel the action link = Link(**value) self.data[link.id] = link raise cherrypy.HTTPRedirect(”/”) tmp = loader.load(”submit.html”) streamValue = tmp.generate() return streamValue.render(”html”, doctype=”html”) Step 9 − The code to be included in submit.html is as follows − <!DOCTYPE html> <head> <title>Input the new link</title> </head> <body class = “submit”> <div id = ” header”> <h1>Submit new link</h1> </div> <form action = “” method = “post”> <table summary = “”> <tr> <th><label for = ” username”>Your name:</label></th> <td><input type = ” text” id = ” username” name = ” username” /></td> </tr> <tr> <th><label for = ” url”>Link URL:</label></th> <td><input type = ” text” id=” url” name= ” url” /></td> </tr> <tr> <th><label for = ” title”>Title:</label></th> <td><input type = ” text” name = ” title” /></td> </tr> <tr> <td></td> <td> <input type = ” submit” value = ” Submit” /> <input type = ” submit” name = ” cancel” value = “Cancel” /> </td> </tr> </table> </form> <div id = “footer”> </div> </body> </html> Step 10 − You will receive the following output − Here, the method name is defined as “POST”. It is always important to cross verify the method specified in the file. If the method includes “POST” method, the values should be rechecked in the database in appropriate fields. If the method includes “GET” method, the values to be saved will be visible in the URL. Print Page Previous Next Advertisements ”;
Bulit-in Http Server
Built-in Http Server & Internal Engine ”; Previous Next CherryPy comes with its own web (HTTP) server. That is why CherryPy is self-contained and allows users to run a CherryPy application within minutes of getting the library. The web server acts as the gateway to the application with the help of which all the requests and responses are kept in track. To start the web server, a user must make the following call − cherryPy.server.quickstart() The internal engine of CherryPy is responsible for the following activities − Creation and management of request and response objects. Controlling and managing the CherryPy process. CherryPy – Configuration The framework comes with its own configuration system allowing you to parameterize the HTTP server. The settings for the configuration can be stored either in a text file with syntax close to the INI format or as a complete Python dictionary. To configure the CherryPy server instance, the developer needs to use the global section of the settings. global_conf = { ”global”: { ”server.socket_host”: ”localhost”, ”server.socket_port”: 8080, }, } application_conf = { ”/style.css”: { ”tools.staticfile.on”: True, ”tools.staticfile.filename”: os.path.join(_curdir, ”style.css”), } } This could be represented in a file like this: [global] server.socket_host = “localhost” server.socket_port = 8080 [/style.css] tools.staticfile.on = True tools.staticfile.filename = “/full/path/to.style.css” HTTP Compliance CherryPy has been evolving slowly but it includes the compilation of HTTP specifications with the support of HTTP/1.0 later transferring with the support of HTTP/1.1. CherryPy is said to be conditionally compliant with HTTP/1.1 as it implements all the must and required levels but not all the should levels of the specification. Therefore, CherryPy supports the following features of HTTP/1.1 − If a client claims to support HTTP/1.1, it must send a header field in any request made with the specified protocol version. If it is not done, CherryPy will immediately stop the processing of the request. CherryPy generates a Date header field which is used in all configurations. CherryPy can handle response status code (100) with the support of clients. CherryPy”s built-in HTTP server supports persistent connections that are the default in HTTP/1.1, through the use of the Connection: Keep-Alive header. CherryPy handles correctly chunked requests and responses. CherryPy supports requests in two distinct ways − If-Modified-Since and If-Unmodified-Since headers and sends responses as per the requests accordingly. CherryPy allows any HTTP method. CherryPy handles the combinations of HTTP versions between the client and the setting set for the server. Multithreaded Application Server CherryPy is designed based on the multithreading concept. Every time a developer gets or sets a value into the CherryPy namespace, it is done in the multi-threaded environment. Both cherrypy.request and cherrypy.response are thread-data containers, which imply that your application calls them independently by knowing which request is proxied through them at runtime. Application servers using the threaded pattern are not highly regarded because the use of threads is seen as increasing the likelihood of problems due to synchronization requirements. The other alternatives include − Multi-process Pattern Each request is handled by its own Python process. Here, performance and stability of the server can be considered as better. Asynchronous Pattern Here, accepting new connections and sending the data back to the client is done asynchronously from the request process. This technique is known for its efficiency. URL Dispatching The CherryPy community wants to be more flexible and that other solutions for dispatchers would be appreciated. CherryPy 3 provides other built-in dispatchers and offers a simple way to write and use your own dispatchers. Applications used to develop HTTP methods. (GET, POST, PUT, etc.) The one which defines the routes in the URL – Routes Dispatcher HTTP Method Dispatcher In some applications, URIs are independent of the action, which is to be performed by the server on the resource. For example,http://xyz.com/album/delete/10 The URI contains the operation the client wishes to carry out. By default, CherryPy dispatcher would map in the following way − album.delete(12) The above mentioned dispatcher is mentioned correctly, but can be made independent in the following way − http://xyz.com/album/10 The user may wonder how the server dispatches the exact page. This information is carried by the HTTP request itself. When there is request from client to server, CherryPy looks the best suiting handler, the handler is representation of the resource targeted by the URI. DELETE /album/12 HTTP/1.1 Routes Dispatcher Here is a list of the parameters for the method required in dispatching − The name parameter is the unique name for the route to connect. The route is the pattern to match URIs. The controller is the instance containing page handlers. Using the Routes dispatcher connects a pattern that matches URIs and associates a specific page handler. Example Let us take an example to understand how it works − import random import string import cherrypy class StringMaker(object): @cherrypy.expose def index(self): return “Hello! How are you?” @cherrypy.expose def generate(self, length=9): return ””.join(random.sample(string.hexdigits, int(length))) if __name__ == ”__main__”: cherrypy.quickstart(StringMaker ()) Follow the steps given below to get the output of the above code − Step 1 − Save the above mentioned file as tutRoutes.py. Step 2 − Visit the following URL − http://localhost:8080/generate?length=10 Step 3 − You will receive the following output − Print Page Previous Next Advertisements ”;