CherryPy

Pythonic, object-oriented web framework

http://www.cherrypy.org/

Install on Slackware 14

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

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 
  12     @cherrypy.expose
  13     def add(self,param1,param2):
  14        return str( int(param1)+int(param2) )
  15 
  16     @cherrypy.expose
  17     @cherrypy.tools.json_in()
  18     @cherrypy.tools.json_out()
  19     def hellojson(self): 
  20         #  curl -X POST -d "{\"name\":\"jkl\"}" http://localhostcherrypytest/hellojson --header "Content-Type:application/json"
  21         inj = cherrypy.request.json
  22         return {"message": "hello world " + inj['name'] }
  23 
  24 hello = HelloWorld()    
  25 #static dir
  26 confx={'/static': {'tools.staticdir.on':True ,
  27                   'tools.staticdir.dir':'/var/www/htdocs/cherrypytest/static'
  28                   }}
  29 
  30 application = cherrypy.Application(hello, script_name=None, config=confx)

Create static file

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

Restart apache

Open the following URLs:

CherryPy configuration location

In Slack14 is in

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

Templating

Check Python/Jinja2

REST

https://cherrypy.readthedocs.org/en/3.2.6/progguide/REST.html

REST quick tips: http://www.restapitutorial.com/lessons/restquicktips.html

Install python 3.8.5 in raspberry py

Console app

   1 import cherrypy
   2 import wsgiref.handlers
   3 
   4 class RootUrl:
   5   @cherrypy.expose
   6   def index(self):
   7     return "Hello world!"
   8 
   9 class Middle:
  10   @cherrypy.expose
  11   def index(self):
  12     return "middle"
  13 
  14 class GoodbyeWorld:
  15    @cherrypy.expose
  16    def index(self,num=None):
  17      return "Goodbye World!"
  18 
  19 def main():
  20   ru = RootUrl()
  21   ru.middle = Middle()
  22   ru.middle.goodbye  = GoodbyeWorld()
  23   cherrypy.server.socket_host = '0.0.0.0'
  24   cherrypy.quickstart(ru) 
  25 
  26 if __name__ == '__main__':
  27   main()

Docker + cherrypy + ubuntu

   1 FROM ubuntu:latest
   2 RUN apt update
   3 RUN apt install -y nano vim python curl
   4 RUN curl https://bootstrap.pypa.io/pip/2.7/get-pip.py > get-pip.py
   5 RUN python get-pip.py
   6 RUN pip install cherrypy routes --user
   7 RUN mkdir /app
   8 COPY . /app/
   9 WORKDIR /app
  10 CMD ["python","main.py"]

   1 import cherrypy
   2 import wsgiref.handlers
   3 
   4 class RootUrl:
   5   @cherrypy.expose
   6   def index(self):
   7     return "Hello world!"
   8 
   9 class Middle:
  10   @cherrypy.expose
  11   def index(self):
  12     return "middle"
  13 
  14 class GoodbyeWorld:
  15    @cherrypy.expose
  16    def index(self,num=None):
  17      return "Goodbye World!"
  18 
  19 def main():
  20   ru = RootUrl()
  21   ru.middle = Middle()
  22   ru.middle.goodbye  = GoodbyeWorld()
  23   cherrypy.server.socket_host = '0.0.0.0'
  24   cherrypy.quickstart(ru) 
  25 
  26 if __name__ == '__main__':
  27   main()

Python/CherryPy (last edited 2021-03-18 20:11:54 by localhost)