Thursday, February 10, 2005

ISAPI-WSGI - Runs a CherryPy2 application

In my ongoing testing of ISAPI-WSGI, with a Subversion checkout of CherryPy2 I have some of the tutorials running as an ISAPI extension. Only issue discovered relates to how CherryPy handles an empty PATH_INFO but I am sure this will be resolved soon. For the time being the url to access the example must have a trailing backslash. This has now been resolved. Thanks to Remco and Remi for helping me with it.

So now that I have proved CherryPy2 can work with ISAPI-WSGI, Subway should be able to run as an ISAPI extension.

I will add the example to ISAPI-WSGI subversion repository tomorrow. Below is how to get tutorial 3 working:


# Demo of tutorial 3 running cherrypy2 under isapi-wsgi
#
# Executing this script (or any server config script) will install the extension
# into your web server and will create a "loader" DLL _cherrypy.dll in the
# current directory. As the server executes, the PyISAPI framework will load
# this module and create the Extension object.
# A Virtual Directory named "isapi-wsgi-cherrypy" is setup. This dir has the ISAPI
# WSGI extension as the only application, mapped to file-extension '*'.
# Therefore, isapi_wsgi extension handles *all* requests in this directory.
#
# To launch this application from a web browser use a url similar to:
#
# http://localhost/isapi-wsgi-cherrypy/test/
#
# NOTE: Due to an issue in how CherryPy2 currently handles PATH_INFO
# the url must have the trailing /

import isapi_wsgi
import cherrypy
from cherrypy import wsgiapp
from cherrypy import cpg

class WelcomePage:

def index(self):
# Ask for the user's name.
return '''
<form action="greetUser" method="get">
What is your name?
<input style="BACKGROUND-COLOR: #ffffa0" name="name">
<input type="submit" value="Submit Query">
</form>
'''

index.exposed = True


def greetUser(self, name = None):
# CherryPy passes all GET and POST variables as method parameters.
# It doesn't make a difference where the variables come from, how
# large their contents are, and so on.
#
# You can define default parameter values as usual. In this
# example, the "name" parameter defaults to None so we can check
# if a name was actually specified.

if name:
# Greet the user!
return "Hey %s, what's up?" % name
else:
# No name was specified
return 'Please enter your name <a href="./">here</a>.'

greetUser.exposed = True


class CherryPyWsgiApp:
cpg.root = WelcomePage()
wsgiapp.init(configDict = {'socketPort': 80, 'sessionStorageType': 'ram'})
def __call__(self, environ, start_response):
return wsgiapp.wsgiApp(environ, start_response)

# The entry points for the ISAPI extension.
def __ExtensionFactory__():
return isapi_wsgi.ISAPISimpleHandler(test = CherryPyWsgiApp())

if __name__=='__main__':
# If run from the command-line, install ourselves.
from isapi.install import *
params = ISAPIParameters()
# Setup the virtual directories - this is a list of directories our
# extension uses - in this case only 1.
# Each extension has a "script map" - this is the mapping of ISAPI
# extensions.
sm = [
ScriptMapParams(Extension="*", Flags=0)
]
vd = VirtualDirParameters(Name="isapi-wsgi-cherrypy",
Description = "ISAPI-WSGI Cherrypy Test",
ScriptMaps = sm,
ScriptMapUpdate = "replace"
)
params.VirtualDirs = [vd]
HandleCommandLine(params)

No comments: