[Web-SIG] WSGI Utils & SCGI/Quixote.
Titus Brown
titus at caltech.edu
Tue Nov 30 20:01:54 CET 2004
Hi, Colin et al.,
I tried out my Quixote (application) and SCGI (server) adapters with
your wsgiUtils package this morning. With a few tweaks, everything
worked; hooray!
Thanks for making wsgiutils available; it's nice to know that my code
actually works with someone else's ;).
The SCGI server adapter ("SWAP") worked out of the box with both of your
app objects, e.g.
---
class TestAppHandler(swap.SWAP):
def __init__(self, *args, **kwargs):
print 'creating new TestAppHandler'
self.prefix = '/canal' # just my setup...
### hook into wsgiUtils
adaptor = wsgiAdaptor.wsgiAdaptor (CalcApp(), 'siteCookieKey',
calcclient)
self.app_obj = adaptor.wsgiHook
###
swap.SWAP.__init__(self, *args, **kwargs)
if __name__ == '__main__':
scgi_server.SCGIServer(TestAppHandler, port=4000).serve()
---
The Quixote app adapter ("QWIP") took a little more time, but it now
works like so:
---
demo_obj = qwip.QWIP('quixote.demo')
server = wsgiServer.WSGIServer (('issola.caltech.edu', 1088),
{'/demo': demo_obj})
server.serve_forever()
---
The only real problem in getting this to work was that wsgiServer.py
expected *every* URL under /demo to be registered to demo_obj. I
changed the wsgiServer.py code to allow for partial matches & munged
the SCRIPT_NAME and PATH_INFO variables appropriately. I also added
REQUEST_URI because Quixote uses it for a few things; this should
probably be moved into QWIP.
A context-diff of my changes to wsgiServer.py is attached, for your
enjoyment ;).
My experience highlights an issue that needs to be dealt with by any
WSGI server code. Several app frameworks -- Quixote Webware, and Zope,
for example -- expect to be handed control of an entire URL tree.
Moreover this needs to be signalled appropriately via SCRIPT_NAME and
PATH_INFO. Colin, I'd be happy to test whatever system you come up
with... although the quixote.demo code (attached) should work with a
simple Quixote install.
cheers,
--titus
p.s. http://issola.caltech.edu/~t/transfer/qwsgi/README.html
http://issola.caltech.edu/~t/transfer/qwip-and-swap-26.11.04.tar.gz
p.p.s. full test scripts + full modified wsgiServer.py attached.
-------------- next part --------------
*** wsgiServer.py 2004-11-30 10:52:38.000000000 -0800
--- ../WSGI Utils-0.2/lib/wsgiutils/wsgiServer.py 2004-11-03 19:44:10.000000000 -0800
***************
*** 33,51 ****
import SimpleHTTPServer, SocketServer, BaseHTTPServer, urlparse
import sys, logging
- def get_wsgi_app(app_dict, path):
- if app_dict.has_key(path): # exact match
- return (path, app_dict[path])
-
- keys = app_dict.keys()
- keys.sort()
- keys.reverse() # so that /url/suburl is before /url
- for url in keys:
- if path.find(url) == 0:
- return (url, app_dict[url])
-
- return (None, None)
-
class WSGIHandler (SimpleHTTPServer.SimpleHTTPRequestHandler):
def log_message (self, *args):
pass
--- 33,38 ----
***************
*** 56,90 ****
def do_GET (self):
protocol, host, path, parameters, query, fragment = urlparse.urlparse ('http://dummyhost%s' % self.path)
logging.info ("Received GET for path %s" % path)
!
! (app_path, app) = get_wsgi_app(self.server.wsgiApplications, path)
!
! if (not app):
# Not a request for an application, just a file.
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET (self)
return
!
! self.runWSGIApp (app, app_path, path, query)
def do_POST (self):
protocol, host, path, parameters, query, fragment = urlparse.urlparse ('http://dummyhost%s' % self.path)
logging.info ("Received POST for path %s" % path)
!
! (app_path, app) = get_wsgi_app(self.server.wsgiApplications, path)
!
! if (not app):
# We don't have an application corresponding to this path!
self.send_error (404, 'Application not found.')
return
! self.runWSGIApp (app, app_path, path, query)
!
! def runWSGIApp (self, application, path_head, full_path, query):
! logging.info ("Running application for path %s" % full_path)
!
! # pick off that which matches the app path head.
! path_tail = full_path[len(path_head):]
!
env = {'wsgi.version': (1,0)
,'wsgi.url_scheme': 'http'
,'wsgi.input': self.rfile
--- 43,65 ----
def do_GET (self):
protocol, host, path, parameters, query, fragment = urlparse.urlparse ('http://dummyhost%s' % self.path)
logging.info ("Received GET for path %s" % path)
! if (not self.server.wsgiApplications.has_key (path)):
# Not a request for an application, just a file.
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET (self)
return
! self.runWSGIApp (self.server.wsgiApplications [path], path, query)
def do_POST (self):
protocol, host, path, parameters, query, fragment = urlparse.urlparse ('http://dummyhost%s' % self.path)
logging.info ("Received POST for path %s" % path)
! if (not self.server.wsgiApplications.has_key (path)):
# We don't have an application corresponding to this path!
self.send_error (404, 'Application not found.')
return
+ self.runWSGIApp (self.server.wsgiApplications [path], path, query)
! def runWSGIApp (self, application, path, query):
! logging.info ("Running application for path %s" % path)
env = {'wsgi.version': (1,0)
,'wsgi.url_scheme': 'http'
,'wsgi.input': self.rfile
***************
*** 93,101 ****
,'wsgi.multiprocess': 0
,'wsgi.run_once': 0
,'REQUEST_METHOD': self.command
! ,'SCRIPT_NAME': path_head
! ,'PATH_INFO': path_tail
! ,'REQUEST_URI' : full_path
,'QUERY_STRING': query
,'CONTENT_TYPE': self.headers.get ('Content-Type', '')
,'CONTENT_LENGTH': self.headers.get ('Content-Length', '')
--- 68,75 ----
,'wsgi.multiprocess': 0
,'wsgi.run_once': 0
,'REQUEST_METHOD': self.command
! ,'SCRIPT_NAME': path
! ,'PATH_INFO': ''
,'QUERY_STRING': query
,'CONTENT_TYPE': self.headers.get ('Content-Type', '')
,'CONTENT_LENGTH': self.headers.get ('Content-Length', '')
-------------- next part --------------
#! /usr/bin/env python2.3
import sys
sys.path.insert(0, "/u/t/dev/qwsgi/")
from wsgiutils import wsgiServer
import qwip
demo_obj = qwip.QWIP('quixote.demo')
server = wsgiServer.WSGIServer (('issola.caltech.edu', 1088),
{'/demo': demo_obj})
server.serve_forever()
-------------- next part --------------
#!/usr/bin/env python2.3
from wsgiutils import SessionClient, wsgiAdaptor
import sys
import time
import os
import getopt
from scgi import scgi_server
import swap
class TestApp:
def requestHandler (self, request):
# This is a multi-threaded area, we must be thread safe.
request.setContentType ('text/html')
session = request.getSession()
if (session.has_key ('lastRequestTime')):
lastRequest = session ['lastRequestTime']
else:
lastRequest = None
thisTime = time.time()
session ['lastRequestTime'] = thisTime
# Use some templating library to generate some output
if (lastRequest is None):
return "<html><body><h1>The first request!</h1></body></html>"
else:
return "<html><body><h1>The time is %s, last request was at %s</h1></body></html>" % (str (thisTime), str (lastRequest))
class CalcApp:
""" A simple calculator app that uses a username/password of 'user/user' and demonstrates forms.
"""
def requestHandler (self, request):
request.setContentType ('text/html')
# Authenticate the user
username = request.getUsername()
password = request.getPassword()
if (username is None or username != 'user'):
request.unauthorisedBasic ("Calculator")
return ""
if (password is None or password != 'user'):
request.unauthorisedBasic ("Calculator")
return ""
# We have a valid user, so get the form entries
formData = request.getFormFields()
try:
firstValue = float (formData.getfirst ('value1', "0"))
secondValue = float (formData.getfirst ('value2', "0"))
except:
# No valid numbers, try again
return self.displayForm(request, 0)
# Display the sum
return self.displayForm (request, firstValue + secondValue)
def displayForm (self, request, sumValue):
return """<html><body><h1>Calculator</h1>
<h2>Last answer was: %s</h2>
<form name="calc">
<input name="value1" type="text"><br>
<input name="value2" type="text">
<button name="Calculate" type="submit">Cal.</button>
</form>
</body></html>""" % str (sumValue)
testclient = SessionClient.LocalSessionClient('session.dbm', 'testappid')
testadaptor = wsgiAdaptor.wsgiAdaptor (TestApp(), 'siteCookieKey', testclient)
calcclient = SessionClient.LocalSessionClient ('calcsession.dbm', 'calcid')
calcAdaptor = wsgiAdaptor.wsgiAdaptor (CalcApp(), 'siteCookieKey', calcclient)
class TestAppHandler(swap.SWAP):
def __init__(self, *args, **kwargs):
print 'creating new TestAppHandler'
self.prefix = '/canal' # just my setup...
self.app_obj = wsgiAdaptor.wsgiAdaptor (CalcApp(), 'siteCookieKey', calcclient).wsgiHook
swap.SWAP.__init__(self, *args, **kwargs)
if __name__ == '__main__':
scgi_server.SCGIServer(TestAppHandler, port=4000).serve()
-------------- next part --------------
""" wsgiServer
Copyright (c) 2004 Colin Stewart (http://www.owlfish.com/)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
If you make any bug fixes or feature enhancements please let me know!
A basic multi-threaded WSGI server.
"""
import SimpleHTTPServer, SocketServer, BaseHTTPServer, urlparse
import sys, logging
def get_wsgi_app(app_dict, path):
if app_dict.has_key(path): # exact match
return (path, app_dict[path])
keys = app_dict.keys()
keys.sort()
keys.reverse() # so that /url/suburl is before /url
for url in keys:
if path.find(url) == 0:
return (url, app_dict[url])
return (None, None)
class WSGIHandler (SimpleHTTPServer.SimpleHTTPRequestHandler):
def log_message (self, *args):
pass
def log_request (self, *args):
pass
def do_GET (self):
protocol, host, path, parameters, query, fragment = urlparse.urlparse ('http://dummyhost%s' % self.path)
logging.info ("Received GET for path %s" % path)
(app_path, app) = get_wsgi_app(self.server.wsgiApplications, path)
if (not app):
# Not a request for an application, just a file.
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET (self)
return
self.runWSGIApp (app, app_path, path, query)
def do_POST (self):
protocol, host, path, parameters, query, fragment = urlparse.urlparse ('http://dummyhost%s' % self.path)
logging.info ("Received POST for path %s" % path)
(app_path, app) = get_wsgi_app(self.server.wsgiApplications, path)
if (not app):
# We don't have an application corresponding to this path!
self.send_error (404, 'Application not found.')
return
self.runWSGIApp (app, app_path, path, query)
def runWSGIApp (self, application, path_head, full_path, query):
logging.info ("Running application for path %s" % full_path)
# pick off that which matches the app path head.
path_tail = full_path[len(path_head):]
env = {'wsgi.version': (1,0)
,'wsgi.url_scheme': 'http'
,'wsgi.input': self.rfile
,'wsgi.errors': sys.stderr
,'wsgi.multithread': 1
,'wsgi.multiprocess': 0
,'wsgi.run_once': 0
,'REQUEST_METHOD': self.command
,'SCRIPT_NAME': path_head
,'PATH_INFO': path_tail
,'REQUEST_URI' : full_path
,'QUERY_STRING': query
,'CONTENT_TYPE': self.headers.get ('Content-Type', '')
,'CONTENT_LENGTH': self.headers.get ('Content-Length', '')
,'REMOTE_ADDR': self.client_address[0]
,'SERVER_NAME': self.server.server_address [0]
,'SERVER_PORT': self.server.server_address [1]
,'SERVER_PROTOCOL': self.request_version
}
for httpHeader, httpValue in self.headers.items():
env ['HTTP_%s' % httpHeader.replace ('-', '_').upper()] = httpValue
# Setup the state
self.wsgiSentHeaders = 0
self.wsgiHeaders = []
# We have the environment, now invoke the application
result = application (env, self.wsgiStartResponse)
for data in result:
if data:
self.wsgiWriteData (data)
if (not self.wsgiSentHeaders):
# We must write out something!
self.wsgiWriteData ("")
return
def wsgiStartResponse (self, response_status, response_headers, exc_info=None):
if (self.wsgiSentHeaders):
raise Exception ("Headers already sent and start_response called again!")
# Should really take a copy to avoid changes in the application....
self.wsgiHeaders = (response_status, response_headers)
return self.wsgiWriteData
def wsgiWriteData (self, data):
if (not self.wsgiSentHeaders):
status, headers = self.wsgiHeaders
# Need to send header prior to data
statusCode = status [:status.find (' ')]
statusMsg = status [status.find (' ') + 1:]
self.send_response (int (statusCode), statusMsg)
for header, value in headers:
self.send_header (header, value)
self.end_headers()
self.wsgiSentHeaders = 1
# Send the data
self.wfile.write (data)
class WSGIServer (SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
def __init__ (self, serverAddress, wsgiApplications):
BaseHTTPServer.HTTPServer.__init__ (self, serverAddress, WSGIHandler)
self.wsgiApplications = wsgiApplications
self.serverShuttingDown = 0
More information about the Web-SIG
mailing list