| 1 | | """Native adapter for serving CherryPy via mod_python""" |
|---|
| | 1 | """Native adapter for serving CherryPy via mod_python |
|---|
| | 2 | |
|---|
| | 3 | Basic usage: |
|---|
| | 4 | |
|---|
| | 5 | ########################################## |
|---|
| | 6 | # Application in a module called myapp.py |
|---|
| | 7 | ########################################## |
|---|
| | 8 | |
|---|
| | 9 | import cherrypy |
|---|
| | 10 | |
|---|
| | 11 | class Root: |
|---|
| | 12 | @cherrypy.expose |
|---|
| | 13 | def index(self): |
|---|
| | 14 | return 'Hi there, Ho there, Hey there' |
|---|
| | 15 | |
|---|
| | 16 | |
|---|
| | 17 | # We will use this method from the mod_python configuration |
|---|
| | 18 | # as the entyr point to our application |
|---|
| | 19 | def setup_server(): |
|---|
| | 20 | cherrypy.tree.mount(Root()) |
|---|
| | 21 | cherrypy.config.update({'environment': 'production', |
|---|
| | 22 | 'log.screen': False, |
|---|
| | 23 | 'show_tracebacks': False}) |
|---|
| | 24 | # You must start the engine in a non-blocking fashion |
|---|
| | 25 | # so that mod_python can proceed |
|---|
| | 26 | cherrypy.engine.start(blocking=False) |
|---|
| | 27 | |
|---|
| | 28 | ########################################## |
|---|
| | 29 | # mod_python settings for apache2 |
|---|
| | 30 | # This should reside in your httpd.conf |
|---|
| | 31 | # or a file that will be loaded at |
|---|
| | 32 | # apache startup |
|---|
| | 33 | ########################################## |
|---|
| | 34 | |
|---|
| | 35 | # Start |
|---|
| | 36 | DocumentRoot "/" |
|---|
| | 37 | Listen 8080 |
|---|
| | 38 | LoadModule python_module /usr/lib/apache2/modules/mod_python.so |
|---|
| | 39 | |
|---|
| | 40 | <Location "/"> |
|---|
| | 41 | PythonPath "sys.path+['/path/to/my/application']" |
|---|
| | 42 | SetHandler python-program |
|---|
| | 43 | PythonHandler cherrypy._cpmodpy::handler |
|---|
| | 44 | PythonOption cherrypy.setup myapp::setup_server |
|---|
| | 45 | PythonDebug On |
|---|
| | 46 | </Location> |
|---|
| | 47 | # End |
|---|
| | 48 | |
|---|
| | 49 | The actual path to your mod_python.so is dependant of your |
|---|
| | 50 | environment. In this case we suppose a global mod_python |
|---|
| | 51 | installation on a Linux distribution such as Ubuntu. |
|---|
| | 52 | |
|---|
| | 53 | We do set the PythonPath configuration setting so that |
|---|
| | 54 | your application can be found by from the user running |
|---|
| | 55 | the apache2 instance. Of course if your application |
|---|
| | 56 | resides in the global site-package this won't be needed. |
|---|
| | 57 | |
|---|
| | 58 | Then restart apache2 and access http://localhost:8080 |
|---|
| | 59 | """ |
|---|