This repository has been archived by the owner on Oct 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathScgiWsgi
208 lines (157 loc) · 5.88 KB
/
ScgiWsgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
= SCGI WSGI =
This is a HOWTO. See BehindApache for a higher-level discussion.
A very simple setup lets your cherry run under SCGI (on apache in my setup). You need just a running apache server with [http://www.mems-exchange.org/software/scgi/ mod_scgi] and have the scgi python module installed. You also need an SCGI front end to WSGI. In this case, I'm using the SCGI-->WSGI application proxy, aka "SWAP" from [http://pythonpaste.org/ Python Paste]. Here's a direct link to [http://pythonpaste.org/trac/file/Paste/trunk/paste/util/scgiserver.py "SWAP"]. If that link doesn't work, you may have to hunt down SWAP on your own.
{{{
#!python
#!/usr/bin/python
import cherrypy
from paste.util.scgiserver import serve_application
class HelloWorld:
def index(self):
return "Hello world!"
index.exposed = True
app = cherrypy.tree.mount(HelloWorld())
cherrypy.engine.start(blocking=False)
serve_application(application=app, prefix="/dynamic", port=4000)
}}}
The Apache configuration that goes along with this is as follows:
{{{
<Location "/dynamic">
SCGIServer 127.0.0.1 4000
SCGIHandler On
</Location>
}}}
Alternatively you may use [http://www.saddi.com/software/news/archives/80-scgi-wsgi-1.1-released.html scgi-wsgi]
The following example uses Python 3.2, lighttpd V1.4.26, scgi-wsgi-py3 V1.1 and CherryPy V3.2
Download [http://www.saddi.com/software/news/archives/80-scgi-wsgi-1.1-released.html scgi-wsgi] I suggest using the latest code from mercurial. Check it out, and follow the instructions in README to build it.
Then create a sample application:
your_webapp.py:
{{{
#!python
#!/usr/bin/python3
import sys
import cherrypy
# Check if we are a child of the SCGI-WSGI interface
SCGIWSGI = (sys.argv[0] == 'scgi-wsgi')
# Default configurations - could get from a file.
ROOTCONFIG={'/':{}}
GLOBALCONFIG={}
# Adjust configurations to suit running under scgi-wsgi or native
if SCGIWSGI:
WEBBASE='/your_webapp'
else:
WEBBASE='/'
GLOBALCONFIG.update({'server.socket_host':"0.0.0.0",
'server.socket_port': 8080,
'server.thread_pool': 10})
class Root:
@cherrypy.expose
def index(self):
return "Hello world!"
class Branch:
@cherrypy.expose
def index(self):
return "Howdy"
@cherrypy.expose
def default(self, attr='abc'):
return attr.upper()
@cherrypy.expose
def leaf(self, size=0):
return str(int(size) + 3)
# Set Global configuration
cherrypy.config.update(GLOBALCONFIG)
# Define our website pages
root = Root()
root.branch = Branch()
app = cherrypy.tree.mount(root, script_name=WEBBASE, config=ROOTCONFIG)
# If running under scgi-wsgi then we dont need the built in http server or auto-reload.
if (SCGIWSGI) :
# Prevent default Built In HTTP Server running.
cherrypy.server.unsubscribe()
# Autorestart doesn't work under scgi-wsgi server.
cherrypy.config.update({'engine.autoreload_on':False})
# Initialise signal handler.
if hasattr(cherrypy.engine, 'signal_handler'):
cherrypy.engine.signal_handler.subscribe()
# Initialise console control
if hasattr(cherrypy.engine, "console_control_handler"):
cherrypy.engine.console_control_handler.subscribe()
# Start the engine.
cherrypy.engine.start()
# Running standalone? if so we need to block.
if (not SCGIWSGI) :
cherrypy.engine.block()
}}}
This application will run standalone with the embedded http server standard with CherryPy. But if you run it:
{{{
./scgi-wsgi -v your_webapp app
}}}
It will execute under the scgi-wsgi server and will be able to interface to Apache or Lighttpd. I only have tested against Lighttpd.
Example Lighttpd configs
10-scgi.conf:
{{{
## SCGI programs have the same functionality as CGI programs,
## but are considerably faster through lower interpreter startup
## time and socketed communication
##
## Documentation: /usr/share/doc/lighttpd-doc/fastcgi.txt.gz
## http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi
server.modules += ( "mod_scgi" )
#debug.log-request-handling = "enable"
#scgi.debug = 1
}}}
50-your_webapp.conf :
{{{
# Alias for your_webapp directory
alias.url += (
"/imbibe" => "/opt/your_webapp/www",
)
$HTTP["url"] =~ "^/your_webapp" {
scgi.server = (
"/" =>
( "127.0.0.1" =>
(
"host" => "127.0.0.1",
"port" => 4000,
"check-local" => "disable",
"docroot" => "/opt/your_webapp/www"
)
)
)
}
}}}
Place these files in your lighttpd "conf-available" directory and enable them with "lighttpd-enable-mod"
restart Lighttpd.
Browse to http://yourserver/your_webapp and you should see the sample running under Lighttpd using SCGI.
{{{
#!html
<h2 class='compatibility'>Older versions</h2>
}}}
== 2.2 ==
This example uses the VirtualPathFilter (not needed in CP 3); feel free to ignore it if you don't need it.
{{{
#!python
#!/usr/bin/python
import cherrypy
from virtualpathfilter import VirtualPathFilter
from cherrypy._cpwsgi import wsgiApp
from paste.util.scgiserver import serve_application
class HelloWorld:
""" Sample request handler class. """
_cpFilterList = [VirtualPathFilter()]
def index(self):
return "Hello world!"
index.exposed = True
cherrypy.root = HelloWorld()
cherrypy.config.update({
'/': {
'server.environment': 'development',
'virtualPathFilter.on': True,
'virtualPathFilter.prefix': "/dynamic"
}
})
# init cp
cherrypy.server.start(initOnly=True, serverClass=None)
# run the server
serve_application(application=wsgiApp, prefix="/dynamic", port=4000)
}}}