[Web-SIG] My port of jonpy to current WSGI draft

Peter Hunt floydophone at gmail.com
Sat Aug 28 03:56:53 CEST 2004


I've taken the liberty to add a jonpy adapter to WSGI. In short, it
works. It's an example of a high-level, servlet interface to WSGI, and
it allows you to write real WSGI apps _now_, and also supports apps
that were written to run on other platforms.

My hello, world executed alright, though I havent sufficiently tested
it yet. From the looks of it, however, I think it's a complete
implementation. The only design issue I see is that it doesn't use
yielding; it is push.

The attached files are:
- wsgicgi.py - the run_with_cgi method described in the pre-PEP,
except I fixed a typo and fixed some issues regarding the blank line
between the headers and content, and commented out the non-standard
Status: header.
- test.cgi - the hello, world test script, taken verbatim from the jonpy website
- jonpy_wsgi.py - the jonpy middleware I wrote


I know there's no unit tests or comments or docs, but it allows many
real world apps to run on WSGI _today_.

Tested on Windows XP, and IIS (so sue me ;) ).
-------------- next part --------------
import os, sys

def run_with_cgi(application):

    environ = {}
    environ.update(os.environ)
    environ['wsgi.input']        = sys.stdin
    environ['wsgi.errors']       = sys.stderr
    environ['wsgi.version']      = '1.0'
    environ['wsgi.multithread']  = False
    environ['wsgi.multiprocess'] = True

    def start_response(status,headers):
        #print "Status:", status
        for key,val in headers:
            print "%s: %s" % (key,val)
        print
        return sys.stdout.write

    result = application(environ, start_response)
    if result:
        try:
            for data in result:
                sys.stdout.write(data)
        finally:
            if hasattr(result,'close'):
                result.close()
               
-------------- next part --------------
from jon import cgi

class WSGIRequest(cgi.Request):
	"""An implementation of Request which is also a WSGI app."""
	def __init__(self, handler):
		cgi.Request.__init__(self, handler)
	def __call__(self, environ, start_response):
		self.environ = environ
		self.stdin = environ['wsgi.input']
		self.start_response = start_response
		self._writefunc = None
		cgi.Request._init(self)
		self.process()
	def process(self):
		"""Execute the handler"""
		self._init()
		try:
			handler = self._handler_type()
		except:
			self.traceback()
		else:
			try:
				handler.process(self)
			except:
				handler.traceback(self)
		self.close()
	def output_headers(self):
		self._writefunc = self.start_response("200 OK", self._headers)
	def error(self, s):
		self.environ['wsgi.error'].write(s)
	def _write(self, s):
		assert self._writefunc != None
		self._writefunc(s)

#def simple_app(environ, start_response):
#     """Simplest possible application object"""
#     status = '200 OK'
#     headers = [('Content-type','text/plain')]
#     write = start_response(status, headers)
#     write('Hello world!\n')

-------------- next part --------------
#!/usr/bin/env python

from jon import cgi
import wsgicgi
import jonpy_wsgi

# bit redundant: cgi->wsgi->cgi

class Handler(cgi.Handler):
	def process(self, req):
		req.set_header("Content-Type", "text/plain")
		req.write("Hello, %s!\n" % req.params.get("greet", "world"))

wsgicgi.run_with_cgi(jonpy_wsgi.WSGIRequest(Handler))


More information about the Web-SIG mailing list