durusmail: quixote-users: updated version of twisted/quixote script
updated version of twisted/quixote script
2002-06-12
2002-06-12
2002-06-12
2002-06-13
updated version of twisted/quixote script
Jon Dyte
2002-06-12
Hi
This is a slightly modified version of Andrew Kuchlings' script,
(posted a while back) to work with Twisted-0.18.0 from
http://www.twistedmatrix.com without the SMTP part.

The only problem I can still see is that of the headers from
Quixote (bar the status) ending up in the webpage.


Jon


"""qserv

Demo of an HTTP server built on top of Twisted Python that serves a
Quixote application..

"""

# created 2002/03/19, AMK

__revision__ = "$Id$"

import string, cStringIO

from quixote.http_request import HTTPRequest
from quixote.publish import Publisher

from twisted.cred.service import Service
from twisted.internet.app import Application
from twisted.mail import mail
from twisted.protocols import protocol, http
from twisted.web import server

# Ports this server will listen on
HTTP_PORT = 8098


import quixote
quixote.enable_ptl()

publisher = Publisher('quixote.demo')
publisher.read_config('quixote.conf')
publisher.setup_logs()

class QuixoteTWRequest (server.Request):
    def process(self):
        environ = self.create_environment()
        ## this seek is important, it doesnt work without it
        self.content.seek(0,0)
        stdin = self.content
        request = HTTPRequest(stdin, environ)
        stdout = cStringIO.StringIO()
        stderr = cStringIO.StringIO()

        publisher.publish(stdin, stdout, stderr, environ)
        output = stdout.getvalue()
        self.write(output)
        self.setResponseCode(request.response.status_code)
        for hdr, value in request.response.headers.items():
            self.setHeader(hdr, value)

        self.finish()

    def create_environment (self):
        host = self.getHeader('host') or self.getHost()
        if ':' in host:
            serverName = string.split(host, ':')[0]
        else:
            serverName = host
        content_type = self.getHeader('content-type')
        typ, remote_addr, remote_port = self.transport.getPeer()

        env = {
               "PATH_INFO":         self.path,
               "SERVER_SOFTWARE":   server.version,
               "SERVER_NAME":       serverName,
               "GATEWAY_INTERFACE": "CGI/1.1",
               "REMOTE_ADDR":       remote_addr,
               "REMOTE_PORT":       str(remote_port),
               "SERVER_PROTOCOL":   self.clientproto,
               "SERVER_PORT":       str(HTTP_PORT),
               "REQUEST_METHOD":    self.method,
               "SCRIPT_NAME":       "",
               "SCRIPT_FILENAME":   "",
               "REQUEST_URI":       self.uri,
               "CONTENT_TYPE":      content_type,
               }
        return env


class QuixoteFactory (http.HTTPFactory):
    def buildProtocol (self, addr):
        h = http.HTTPChannel()
        h.requestFactory = QuixoteTWRequest
        h.factory = self
        return h


def run ():
    app = Application('Quixote')
    app.listenTCP(HTTP_PORT, QuixoteFactory())
    app.run(save=0)


if __name__ == '__main__':
    run()



reply