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
Michael Smith
2002-06-12
Jon,

I found the same problem when I was playing around with qserv. What's
happening is that both TM and Quixote are writing headers to the
response. The blank line between the Matrix headers and the Quixote
headers causes the Quixote headers to be treated as part of the
returning document.

My solution was the following post-publish() code:

        publisher.publish(stdin, stdout, stderr, environ)
        output = stdout.getvalue()

        #self.setResponseCode(request.response.status_code)
        #for hdr, value in request.response.headers.items():
        #    self.setHeader(hdr, value)
        self.startedWriting = 1
        self.write(output.replace("Status:", "HTTP/1.1"))
        self.finish()

The commented out parts prevent the Matrix headers from going into the
response. You then have to call the set "startWriting" to make Matrix
think the headers have already been written. There's also a "convert
CGI status to http status" hack as well.

Mike Smith

On Wed, Jun 12, 2002 at 10:16:55PM +0100, Jon Dyte wrote:
> 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()
>
>
> _______________________________________________
> Quixote-users mailing list
> Quixote-users@mems-exchange.org
> http://www.mems-exchange.org/mailman/listinfo/quixote-users



reply