Install the CherryPy Python Web Framework

1 Reply • Leave your reply

Introduction

There are many options available for Python developers who want to use Python for web projects. CherryPy is a minimalist, object-oriented web framework which is an excellent choice for programmers who want a quick, easy way to develop object-oriented Python programs for the web.

Related: Use mod_wsgi to Run Python as a Web Application on CentOS 7

Requirements

  • A Cloud Server with Python and Pip installed.

For step-by-step instructions on installing Pip, see our article Use Pip to Install Python Packages.

Note: Python is installed and running on a Standard Linux installation by default. If your server was created with a Minimal installation, you will need to install and configure Python before you proceed.

Firewall Access

You will need to allow access for port 8080 in your default Firewall Policy.

For more information about creating and modifying the Cloud Server Firewall Policy, see using a firewall on a Cloud Server, see our article Configure the Firewall Policy.

Install CherryPy

The simplest way to install CherryPy is with the Pip package management system:

pip install cherrypy

After the installation is complete, you can verify the installation with the command:

python -m cherrypy.tutorial.tut01_helloworld

This will respond with:

[18/Jul/2018:17:27:10] ENGINE Listening for SIGHUP.
[18/Jul/2018:17:27:10] ENGINE Listening for SIGTERM.
[18/Jul/2018:17:27:10] ENGINE Listening for SIGUSR1.
[18/Jul/2018:17:27:10] ENGINE Bus STARTING
[18/Jul/2018:17:27:10] ENGINE Started monitor thread 'Autoreloader'.
[18/Jul/2018:17:27:10] ENGINE Serving on http://127.0.0.1:8080
[18/Jul/2018:17:27:10] ENGINE Bus STARTED

View CherryPy Scripts from the Internet

By default, CherryPy will bind to the localhost IP address 127.0.0.1. In order to view a CherryPy script on your server's public IP address in a browser, you will need to add the following configuration line to your Python script:

cherrypy.server.socket_host = '0.0.0.0'

For example, here is a version of the official CherryPy test script, modified to be viewed on the public Internet:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.server.socket_host = '0.0.0.0' 
cherrypy.quickstart(HelloWorld())

Save this script as test-ip.py. Run the script with the command:

python test-ip.py

The server will respond with:

[18/Jul/2018:17:18:26] ENGINE Listening for SIGHUP.
[18/Jul/2018:17:18:26] ENGINE Listening for SIGTERM.
[18/Jul/2018:17:18:26] ENGINE Listening for SIGUSR1.
[18/Jul/2018:17:18:26] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.

[18/Jul/2018:17:18:26] ENGINE Started monitor thread 'Autoreloader'.
[18/Jul/2018:17:18:26] ENGINE Serving on http://0.0.0.0:8080
[18/Jul/2018:17:18:26] ENGINE Bus STARTED

As you can see, the script is now being served on http://0.0.0.0:8080. This means that you can view it in a browser with either your domain name or the server's IP address:

http://example.com:8080
http://192.168.0.1:8080