[Twisted-Python] no 'headers' in server.Request instance problem

Jon Dyte jon at totient.demon.co.uk
Thu Jun 13 13:51:17 EDT 2002


glyph at twistedmatrix.com wrote :-> From: Itamar Shtull-Trauring <twisted at itamarst.org>
> Subject: Re: [Twisted-Python] no 'headers' in server.Request instance problem
> Date: Thu, 13 Jun 2002 01:37:41 +0200
> 
> > BTW - you don't need to use twisted.web.server.Request - since the 
> > refactor to HTTP you only need twisted.protocols.http.Request, no need 
> > to use twisted.web unless you are using it as a web framework.
> 
> Actually you don't need to use Request at all.  If you want this to work with
> other twisted.web servers, it would be best to structure your work as additions
> to Resource, not Request.
> 
> What functionality are you adding at the Request level?
> 
> 
> -- 
>  |    <`'>    |  Glyph Lefkowitz: Traveling Sorcerer  |
>  |   < _/ >   |  Lead Developer,  the Twisted project  |
>  |  < ___/ >  |      http://www.twistedmatrix.com      |


I'm not really adding any functionality. I was merely making a
previous Twisted/Quixote script work with Twisted-0.18.0, which is how
I found the reported problem. This script is so short I include it
here.

Jon


"""qserv

Demo of an HTTP server built on top of Twisted Python that serves a
Quixote application, and also supports an SMTP interface.

"""

# 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
SMTP_PORT = 8099

import quixote
quixote.enable_ptl()

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

class QuixoteTWRequest (http.Request):
    def process(self):
        environ = self.create_environment()
        ## this seek is important
        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()
	## as per message on quixote list
        self.startedWriting = 1
        self.write(output.replace("Status:", "HTTP/1.1"))
        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()




More information about the Twisted-Python mailing list