Looking for cherrypy Answers? Try Ask4KnowledgeBase
Looking for cherrypy Keywords? Try Ask4Keywords

cherrypyGetting started with cherrypy


This section provides an overview of what cherrypy is, and why a developer might want to use it.

It should also mention any large subjects within cherrypy, and link out to the related topics. Since the Documentation for cherrypy is new, you may need to create initial versions of those related topics.

Installation instructions


  • These instructions suppose you have any type of Linux, Unix, Mac with bash or Git-bash Windows.
  • Windows: Download and install Git-bash for Windows, then execute 'bash' from command line.
  • Other shells than bash are fine too, just replace the activate command below with activate.csh or Google: "virtualenv activate your-shell-name".

Before you start, check that Python, virtualenv and pip are installed:

  • $ python --version
  • $ virtualenv --version
  • $ pip --version


Create a directory with your web/app, create environment and install CherryPy package.

  • $ mkdir /develop/myapp/
  • $ cd /develop/myapp/
  • $ virtualenv venv
  • $ source venv/bin/activate
    • On Windows in Git-bash: $ source venv/Scripts/activate
  • (venv) $ pip install cherrypy
  • (venv) $ python
Python 3.5.2 ...
>>> import cherrypy
>>> cherrypy
<module 'cherrypy' from '... venv/site-packages/cherrypy/__init__.py'>

Congratulations! Now you are ready for your first CherryPy application.

File upload with CherryPy

This example consists of three parts:

  • server.py - CherryPy application that can receive and save a file.
  • webpage.html - Example how to upload a file to server.py from a webpage.
  • cli.py - Example how to upload a file to server.py from a command line tool.
  • Bonus - upload.txt - file that you will upload.


#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import cherrypy

config = {
    'global' : {
        'server.socket_host' : '',
        'server.socket_port' : 8080

class App:

    def upload(self, ufile):
        # Either save the file to the directory where server.py is
        # or save the file to a given path:
        # upload_path = '/path/to/project/data/'
        upload_path = os.path.dirname(__file__)

        # Save the file to a predefined filename
        # or use the filename sent by the client:
        # upload_filename = ufile.filename
        upload_filename = 'saved.txt'

        upload_file = os.path.normpath(
            os.path.join(upload_path, upload_filename))
        size = 0
        with open(upload_file, 'wb') as out:
            while True:
                data = ufile.file.read(8192)
                if not data:
                size += len(data)
        out = '''
File received.
Filename: {}
Length: {}
Mime-type: {}
''' .format(ufile.filename, size, ufile.content_type, data)
        return out

if __name__ == '__main__':
    cherrypy.quickstart(App(), '/', config)


<form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="ufile" />
    <input type="submit" />


This example requires Python requests package, however file can be sent to server in plain Python.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import requests

url = ''
files = {'ufile': open('file.txt', 'rb')}

r = requests.post(url, files=files)



Hello! This file was uploaded to CherryPy.

Upload from browser

  • Run $ server.py
  • Open webpage.html in your web browser.
  • After you select file from your drive and submit it, it will be saved as saved.txt .

Upload from command line

  • Open one console and run $ server.py
  • Open another console and run $ cli.py
    • Note: Test file upload.txt should be in the same directory with cli.py
  • File upload.txt should be uploaded and saved as saved.txt .

Hello world in CherryPy

If you have a virtualenv and CherryPy is already installed in it, create a file hello.py :

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import cherrypy

class HelloWorld(object):
    def index(self):
        return 'Hello World!'

    def greet(self, name):
        return 'Hello {}!'.format(name)


Then execute the file: $ hello.py or $ python hello.py . You should see output similar to this:

user@computer /develop/myapp $ python hello.py
[06/Nov/2016:05:58:44] ENGINE Listening for SIGTERM.
[06/Nov/2016:05:58:44] ENGINE Bus STARTING
[06/Nov/2016:05:58:44] ENGINE Set handler for console events.
CherryPy Checker:
The Application mounted at '' has an empty config.

[06/Nov/2016:05:58:44] ENGINE Started monitor thread '_TimeoutMonitor'.
[06/Nov/2016:05:58:44] ENGINE Started monitor thread 'Autoreloader'.
[06/Nov/2016:05:58:45] ENGINE Serving on
[06/Nov/2016:05:58:45] ENGINE Bus STARTED