MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap
Revision 7 as of 2013-12-30 21:23:48
  • Python
  • CherryPy

CherryPy

Pythonic, object-oriented web framework

http://www.cherrypy.org/

Install on Slackware 14

  • wget https://pypi.python.org/packages/source/C/CherryPy/CherryPy-3.2.4.tar.gz

  • cp CherryPy-3.2.4.tar.gz /tmp

  • cd /tmp
  • tar xvzf CherryPy-3.2.4.tar.gz

  • python setup.py build
  • python setup.py install

Check installation

python
Python 2.7.3 (default, Jul  3 2012, 21:16:07) 
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cherrypy
>>> cherrypy.__version__
'3.2.2'
>>> quit()

CherryPy test web app

Create folder the web app

  • mkdir -p /var/www/htdocs/cherrypytest/static

Create file /var/www/htdocs/cherrypytest/cherrypytest.wsgi

   1 import sys
   2 sys.stdout = sys.stderr
   3 import cherrypy
   4 
   5 cherrypy.config.update({'environment': 'embedded'})
   6 
   7 class HelloWorld(object):
   8     @cherrypy.expose
   9     def index(self):
  10         return "Hello World CherryPy!!!!"
  11     @cherrypy.expose
  12     def add(self,param1,param2):
  13        return str( int(param1)+int(param2) )
  14 
  15 hello = HelloWorld()    
  16 #static dir
  17 confx={'/static': {'tools.staticdir.on':True ,
  18                   'tools.staticdir.dir':'/var/www/htdocs/cherrypytest/static'
  19                   }}
  20 
  21 application = cherrypy.Application(hello, script_name=None, config=confx)

Create entry in /etc/httpd/vhosts.conf

   1 <VirtualHost *:80>
   2     ServerName localhostcherrypytest
   3     DocumentRoot "/var/www/htdocs/cherrypytest"
   4     WSGIScriptAlias / /var/www/htdocs/cherrypytest/cherrypytest.wsgi
   5     <Directory "/var/www/htdocs/cherrypytest">
   6       Require local
   7     </Directory>
   8 </VirtualHost>

Add entry in /etc/hosts

  • 127.0.0.1 localhostcherrypytest

Restart apache

  • /etc/rc.d/rc.httpd restart

Open the following URLs:

  • http://localhostcherrypytest/

  • http://localhostcherrypytest/add/3/4

CherryPy configuration location

In Slack14 is in

  • /usr/lib/python2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cpconfig.py

Inside the file look for Config.environments . Run python interactively to check the environments

>>> import cherrypy
>>> envs= cherrypy._cpconfig.environments #dictionary
>>> envs['embedded']
{'checker.on': True, 'engine.SIGTERM': None, 'request.show_mismatched_params': True, 'request.show_tracebacks': True, 'engine.autoreload_on': False, 'tools.log_headers.on': True, 'log.screen': True, 'engine.SIGHUP': None}
>>> envs['production']
{'request.show_tracebacks': False, 'log.screen': False, 'request.show_mismatched_params': False, 'checker.on': False, 'engine.autoreload_on': False, 'tools.log_headers.on': False}

Open url http://localhostcherrypytest/add/1/2a with environment embedded will show a traceback.

To disable the traceback add the follow inside the class HelloWorld

   1 class HelloWorld(object):
   2     _cp_config = {'request.show_tracebacks': False}
   3     @cherrypy.expose
   4     def index(self):
   5         return "Hello World CherryPy!!!!"
   6     @cherrypy.expose
   7     def add(self,param1,param2):
   8        return str( int(param1)+int(param2) )
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01