Table of contents
1.
Introduction
2.
REST — Representational State Transfer
3.
REST Interface through CherryPy
4.
Authentication
5.
Authorization
6.
Structure
7.
Encapsulation
8.
Error Handling
8.1.
Example
8.2.
Output
9.
Frequently Asked Questions
9.1.
What Is Cherrypy?
9.2.
What Is Cherrypy Environment Setup?
9.3.
Is CherryPy open source?
9.4.
Is CherryPy good?
10.
Conclusion
Last Updated: Mar 27, 2024

Give us a REST, cherrypy

Author SHIVANGI MALL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A web service is a collection of web-based elements that facilitates data transfer between applications or systems and incorporates open protocols and standards. It can be used, published, and located online. There are many different kinds of web services, including RWS (RESTful Web Service), WSDL, SOAP, and many more. In this article, we discussed REST — Representational State Transfer, REST Interface through CherryPy, and also discussed tools through which  CherryPy's architecture is implemented.

Introduction

REST — Representational State Transfer

A kind of remote access protocol that sends the state from the client to the server, allowing the state to be changed without having to call external programs.

  • Does not specify any particular structure, encoding, or methods for generating helpful error messages.
  • Performs state transfer activities via HTTP "verbs."
  • Utilising URLs, the resources are each uniquely recognized.
  • It is an API transport layer rather than an API.

REST provides a single way to carry out operations on these resources while maintaining the nomenclature of network resources. At least one identifier is used to identify each resource. If HTTP is used as the foundation for the REST infrastructure, then these identifiers are known as Uniform Resource Identifiers (URIs).

Nowadays, it's common for web apps to expose some type of data model or computing capabilities. Without going into specifics, one tactic is to adhere to Roy T. Fielding's REST principles.

The URI set's subsets are followed in some cases.

Let's walk through a brief example of a very simple web API that only loosely adheres to REST concepts.

import random
import string
import cherrypy
@cherrypy.expose
class StringGeneratorWebService(object):
    @cherrypy.tools.accept(media='text/plain')
    def GET(self):
        return cherrypy.session['mystring']
    def POST(self, length=8):
        some_string = ''.join(random.sample(string.hexdigits, int(length)))
        cherrypy.session['mystring'] = some_string
        return some_string
    def PUT(self, another_string):
        cherrypy.session['mystring'] = another_string
    def DELETE(self):
        cherrypy.session.pop('mystring', None)
if __name__ == '__main__':
    conf = {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.sessions.on': True,
            'tools.response_headers.on': True,
            'tools.response_headers.headers': [('Content-Type', 'text/plain')],
        }
    }
    cherrypy.quickstart(StringGeneratorWebService(), '/', conf)

Save this as tut07.py and execute it as follows:

$ python tut07.py

Let's go over a couple of things in detail before we see it in action. Up until this point, CherryPy had been matching URLs by building a tree of exposed methods. We wish to emphasize the significance of the HTTP methods used in the actual requests in the context of our web API. In order to make them all instantly visible, we defined methods with their names and decorated the class itself using cherrypy.expose.

 

The default mechanism of matching URLs must then be replaced with a method that is aware of the entire HTTP method shenanigans. This is what goes in line 27 when we make an instance of the method dispatcher.
 

Finally, we ensure that GET requests will only be fulfilled for clients who accept that content type by forcing the responses' content type to be text/plain and setting an Accept: text/plain header in the request. However, we only do this for that particular HTTP method since it wouldn't signify much for the others.

 

We won't be utilizing your browser for the duration of this tutorial because we need to use a Python client to properly test our web API.

 

Please execute the following command to install requests:

$ pip install requests

Then fire up a Python terminal and try the following commands:

>>> import requests
>>> s = requests.Session()
>>> r = s.get('http://127.0.0.1:8080/')
>>> r.status_code
500
>>> r = s.post('http://127.0.0.1:8080/')
>>> r.status_code, r.text
(200, u'04A92138')
>>> r = s.get('http://127.0.0.1:8080/')
>>> r.status_code, r.text
(200, u'04A92138')
>>> r = s.get('http://127.0.0.1:8080/', headers={'Accept': 'application/json'})
>>> r.status_code
406
>>> r = s.put('http://127.0.0.1:8080/', params={'another_string': 'hello'})
>>> r = s.get('http://127.0.0.1:8080/')
>>> r.status_code, r.text
(200, u'hello')
>>> r = s.delete('http://127.0.0.1:8080/')
>>> r = s.get('http://127.0.0.1:8080/')
>>> r.status_code
500

The reasons for the first and final 500 responses are because, in the former, it doesn't exist after we've erased it, and in the former, we haven't yet formed a string through POST.

 

You can see how the application responded in lines 12–14 when our client asked for the created string in JSON format. The web API returns the proper HTTP error code because it is set to only support plain text.

REST Interface through CherryPy

Each component of CherryPy's architecture is implemented through RESTful web services using the following tools:

  • Authentication
  • Authorization
  • Structure
  • Encapsulation
  • Error Handling

Authentication

The users we are engaging with can be verified thanks to authentication. Each authentication method can be handled by the tools in CherryPy.

Authorization

Authorization aids in preserving the URI process’s rationality. The method aids in the morphing of objects using user token leads.

Structure

Maintaining an API structure assists in lightening the workload involved in mapping an application's URI. Maintaining a clear and discoverable API is always crucial. The following should be included in the CherryPy framework's API's fundamental structure:

  •  Accounts and User
  • Autoresponder
  • Contact
  • File
  • Folder
  • List and field
  • Message and Batch

Encapsulation

Encapsulation aids in the development of lightweight, legible humans, and client-accessible API. Along with Creation, Retrieval, Update, and Deletion, there are a number of elements that call for API encapsulation.

Error Handling

If the API cannot execute at the specified speed, this process handles any problems. For instance, 403 denotes an illegal request, while 400 denotes a bad request.

For instance, 403 denotes an illegal request, while 400 denotes a bad request.

Example

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())

Output

Frequently Asked Questions

What Is Cherrypy?

A user-friendly interface to the HTTP protocol is provided for Python developers by the Python web framework CherryPy. Another name for it is a web application library.

What Is Cherrypy Environment Setup?

Like most open-source projects, CherryPy is available in packages that can be downloaded and installed using the methods listed below. By means of a Tarball, Make use of easy_install and Subversion.

Is CherryPy open source?

CherryPy is an open-source project, and hence contributions are welcome.

Is CherryPy good?

With its high scalability and flexibility, CherryPy's integration with any common Python library is quick and easy. Although this architecture has a small community, it lacks any focused tools or solutions.

Conclusion

 We hope this article helps you to learn something new. And if you're interested in learning more, see our posts on ReactJs, Learning React JS for Front-End Web DevelopmentReact Js Projects, and Features of React js.

Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.! Feel free to upvote and share this article if it has been helpful for you.

 

Live masterclass