SSchwarzer.com – Softwareentwicklung
für Technik und Wissenschaft
Dr.-Ing. Stefan Schwarzer
Stefan Schwarzer
SSchwarzer.com
Softwareentwicklung für Technik und Wissenschaft
Treffen der LPUG, 2006-03-16
python setup.py install ausführen (ggf. als root)
#! /usr/bin/env python
import cherrypy
class HelloWorld(object):
def index(self):
return ("<html><head><title>Hello world!</title></head>"
"<body>Hello world!</body></html>")
index.exposed = True
if __name__ == '__main__':
cherrypy.root = HelloWorld()
cherrypy.server.start()
__call__-Methode
#! /usr/bin/env python
import cgi
import cherrypy
class HelloForm(object):
def template(self, body):
return ("<html><head><title>CherryPy demonstration</title></head>"
"<body>%s</body></html>") % body
def form(self):
form_body = """
<form action="/confirmation">
Vorname: <input type="text" name="first_name" /><br />
Nachname: <input type="text" name="last_name" /><br />
<input type="submit" value="Ja, so isses" />
</form>
"""
return self.template(form_body)
form.exposed = True
def confirmation(self, first_name, last_name):
first_name = cgi.escape(first_name)
last_name = cgi.escape(last_name)
confirmation_body = "Hallo, %s %s!" % (first_name, last_name)
return self.template(confirmation_body)
confirmation.exposed = True
if __name__ == '__main__':
cherrypy.root = HelloForm()
cherrypy.server.start()
default-Methode
Die default-Methode wird aufgerufen, wenn
CherryPy kein Callable findet, das zum URL passt.
#! /usr/bin/env python
import cgi
import cherrypy
class HelloWorldWithDefault(object):
def template(self, body):
return ("<html><head><title>CherryPy demonstration</title></head>"
"<body>%s</body></html>") % body
def index(self):
return self.template("Hello world!")
index.exposed = True
def default(self, *args, **kwargs):
absolute_path = cherrypy.request.base + cherrypy.request.path
absolute_path = cgi.escape(absolute_path)
cherrypy.response.status = "404 Not Found"
return self.template("Seite %s nicht gefunden!" % absolute_path)
default.exposed = True
if __name__ == '__main__':
cherrypy.root = HelloWorldWithDefault()
cherrypy.server.start()
[/static/sschwarzer_com.css] staticFilter.on = True staticFilter.file = "static/sschwarzer_com.css" ... cherrypy.config.update(file=...)
cherrypy.config.update(
{'global':
{'server.socketPort': 9090,
'server.environment': "production"}
})