You are not logged in Log in Join
You are here: Home » Members » adytumsolutions » PyCon2004 » DonovansTwistedZopeAdaptation » wikipage_view

Log in
Name

Password

 
 
Zope2 Sprint » TwistedZope »

DonovansTwistedZopeAdaptation

# run this via python twisted/scripts/twistd.py -noy thisscriptname.py

import os
import sys
import string
sw = os.path.abspath('/usr/local/zope')
sys.path.insert(0, os.path.join(sw, 'lib', 'python'))
from ZPublisher.Publish import publish_module
from ZServer import HTTPResponse

from twisted.protocols import http


def setupzope():
    import os
    import sys
    here = os.path.abspath('/Users/oubiwann/zope27')
    sw = os.path.abspath('/usr/local/zope')
    sys.path.insert(0, os.path.join(sw, 'lib', 'python'))
    import Zope
    Zope.configure(os.path.join(here, 'etc', 'zope.conf'))
    Zope.startup()

__version__ = 1

setupzope()
from twisted.web import static, server, twcgi, resource
from cStringIO import StringIO


class ZopeResource(resource.Resource):
    def __init__(self, data):
        self.response = data


class ZopeRequest(server.Request):
    def render(self, resource):
        resp = resource.response
        self.transport.write('HTTP/1.1 %s %s\r\n' % (resp.status, http.responses[resp.status]))
        self.transport.write(str(resp))
        self.transport.loseConnection()


class ZopeSite(server.Site):
    requestFactory = ZopeRequest
    def getResourceFor(self, request):
        stdout = StringIO()
        script_name = "/"+string.join(request.prepath, '/')
        python_path = string.join(sys.path, os.pathsep)
        serverName = string.split(request.getRequestHostname(), ':')[0]
        env = {
            "SERVER_SOFTWARE":   'ZTwisted' , # server.version,
            "SERVER_NAME":       serverName,
            "GATEWAY_INTERFACE": "CGI/1.1",
            "SERVER_PROTOCOL":   request.clientproto,
            "SERVER_PORT":       str(request.getHost()[2]),
            "REQUEST_METHOD":    request.method,
            "SCRIPT_NAME":       script_name, # XXX
            "REQUEST_URI":       request.uri,
            "PATH_INFO":         request.path,
            }
        resp = HTTPResponse.HTTPResponse()
        publish_module('Zope', stdout=stdout, environ=env, response=resp)
        return ZopeResource(resp)


from twisted.application import service, internet


application = service.Application("twisted-zope")
internet.TCPServer(6969, ZopeSite(None)).setServiceParent(application)