Size: 6027
Comment:
|
← Revision 35 as of 2025-02-08 14:10:19 ⇥
Size: 7133
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
<<TableOfContents(2)>> |
|
Line 8: | Line 10: |
* 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 * cd CherryPy-3.2.4 * python setup.py build * python setup.py install |
{{{#!highlight bash 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 cd CherryPy-3.2.4 python setup.py build python setup.py install }}} |
Line 17: | Line 21: |
{{{ 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() |
{{{#!highlight python # 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() |
Line 31: | Line 35: |
* mkdir -p /var/www/htdocs/cherrypytest/static | {{{#!highlight bash mkdir -p /var/www/htdocs/cherrypytest/static }}} |
Line 62: | Line 68: |
'tools.staticdir.dir':'/var/www/htdocs/cherrypytest/static' | 'tools.staticdir.dir':'/var/www/htdocs/cherrypytest/static', 'tools.gzip.on': True, 'tools.gzip.mime_types': ['text/*', 'application/*'] |
Line 69: | Line 77: |
* echo "Static Test" > /var/www/htdocs/cherrypytest/static/a.txt | {{{#!highlight bash echo "Static Test" > /var/www/htdocs/cherrypytest/static/a.txt }}} |
Line 87: | Line 97: |
* /etc/rc.d/rc.httpd restart | {{{#!highlight bash /etc/rc.d/rc.httpd restart }}} |
Line 100: | Line 112: |
{{{ >>> 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. |
{{{#!highlight python 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. |
Line 132: | Line 144: |
* tar xvzf Python-3.8.5.tgz * cd Python-3.8.5/ * ./configure * make clean * make * make install * sudo make install * /usr/local/bin/python3.8 -v * /usr/local/bin/pip3.8 install cherrypy |
{{{#!highlight bash tar xvzf Python-3.8.5.tgz cd Python-3.8.5/ ./configure make clean make make install sudo make install /usr/local/bin/python3.8 -v /usr/local/bin/pip3.8 install cherrypy }}} |
Line 146: | Line 160: |
* pip install cherrypy routes --user * python main.py |
{{{#!highlight bash pip install cherrypy routes --user python main.py }}} |
Line 180: | Line 196: |
# build.sh | |
Line 181: | Line 198: |
docker run -p 8181:8080 -d --rm test1-image | mkdir mnt_data docker run -p 8181:8080 -d --rm --mount type=bind,source="$(pwd)"/mnt_data,target=/mnt/data test1-image # what is written in the folder /mnt/data located in the container is shared in the folder mnt_data docker run -p 8282:8080 -d --rm \ --mount type=bind,source="$(pwd)"/mnt_data,target=/mnt/data \ test1-image ab -n 10000 -c 10 http://localhost:8181/ & ab -n 10000 -c 10 http://localhost:8282/ & tail -f mnt_data/filex.log }}} {{{#!highlight bash cd ~ cd Documents mkdir test-cherrypy cd test-cherrypy docker build -t test1-image . docker run -p 8181:8080 -d --rm test1-image # expose internal port 8080 to 8181 |
Line 185: | Line 219: |
curl localhost:8181/middle/goodbye/ | |
Line 187: | Line 222: |
=== Dockerfile === |
|
Line 200: | Line 237: |
=== main.py === | |
Line 203: | Line 241: |
import time import socket |
|
Line 207: | Line 247: |
return "Hello world!" | f=open('/mnt/data/filex.log','a') f.write('%s %f\n'%( socket.gethostname(), time.time())) f.close() return "Hello world ola mundo" |
Contents
CherryPy
Pythonic, object-oriented web framework
Install on Slackware 14
Check installation
CherryPy test web app
Create folder the web app
1 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
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 'tools.gzip.on': True,
29 'tools.gzip.mime_types': ['text/*', 'application/*']
30 }}
31
32 application = cherrypy.Application(hello, script_name=None, config=confx)
Create static file
1 echo "Static Test" > /var/www/htdocs/cherrypytest/static/a.txt
Create entry in /etc/httpd/vhosts.conf
Add entry in /etc/hosts
- 127.0.0.1 localhostcherrypytest
Restart apache
1 /etc/rc.d/rc.httpd restart
Open the following URLs:
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
1 import cherrypy
2 envs= cherrypy._cpconfig.environments #dictionary
3 envs['embedded']
4 # {'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}
5 envs['production']
6 # {'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
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 # build.sh
2 docker build -t test1-image .
3 mkdir mnt_data
4 docker run -p 8181:8080 -d --rm --mount type=bind,source="$(pwd)"/mnt_data,target=/mnt/data test1-image
5 # what is written in the folder /mnt/data located in the container is shared in the folder mnt_data
6 docker run -p 8282:8080 -d --rm \
7 --mount type=bind,source="$(pwd)"/mnt_data,target=/mnt/data \
8 test1-image
9 ab -n 10000 -c 10 http://localhost:8181/ &
10 ab -n 10000 -c 10 http://localhost:8282/ &
11 tail -f mnt_data/filex.log
1 cd ~
2 cd Documents
3 mkdir test-cherrypy
4 cd test-cherrypy
5 docker build -t test1-image .
6 docker run -p 8181:8080 -d --rm test1-image # expose internal port 8080 to 8181
7 curl localhost:8181
8 docker exec -it 94af745958fa bash
9 # edit main.py to change return message
10 curl localhost:8181/middle/goodbye/
11 docker stop 94af745958fa
Dockerfile
main.py
1 import cherrypy
2 import wsgiref.handlers
3 import time
4 import socket
5
6 class RootUrl:
7 @cherrypy.expose
8 def index(self):
9 f=open('/mnt/data/filex.log','a')
10 f.write('%s %f\n'%( socket.gethostname(), time.time()))
11 f.close()
12 return "Hello world ola mundo"
13
14 class Middle:
15 @cherrypy.expose
16 def index(self):
17 return "middle"
18
19 class GoodbyeWorld:
20 @cherrypy.expose
21 def index(self,num=None):
22 return "Goodbye World!"
23
24 def main():
25 ru = RootUrl()
26 ru.middle = Middle()
27 ru.middle.goodbye = GoodbyeWorld()
28 cherrypy.server.socket_host = '0.0.0.0'
29 cherrypy.quickstart(ru)
30
31 if __name__ == '__main__':
32 main()