[Twisted-web] Cheetah+twisted.web2

Andrea Arcangeli andrea at cpushare.com
Tue Jan 17 22:42:46 MST 2006

On Wed, Jan 18, 2006 at 03:56:41AM +0100, Andrea Arcangeli wrote:
> to twisted.web but not to twisted.web2 yet). So I'll do that next.

I did quite an hack to avoid having to update the reverse proxy on the
other end (my modified twisted.web reverse proxy):

class myvhost(object):
	from zope.interface import implements
	from twisted.web2 import iweb

	def __init__(self, uri, res, web_port):
		self.res = res
		self.scheme = 'http'
		self.host = 'klive.cpushare.com'
		self.port = web_port

	def renderHTTP(self, req):
		return http.Response(responsecode.NOT_FOUND)

	def locateChild(self, req, segments):
		req.scheme = self.scheme
		req.host = self.host
		req.port = self.port

		if not req.prepath and segments[0] == 'vhost':
			host, port = segments[3].split(':')
			class remote(object):
				def __init__(self, host, port):
					from twisted.internet import address
					self.client = address.IPv4Address("TCP", host, int(port))
				def getRemoteHost(self):
					return self.client
			req.chanRequest.getRemoteHost = remote(host,port).getRemoteHost
			cut_len = len('/'.join(segments[:4])) + 1
			req.uri = req.uri[cut_len:]
			req.path = req.path[cut_len:]
			segments = segments[4:]

		return self.res, segments

Cheetah over web2 is all productive online at klive.cpushare.com, it's a
100% complete port of the nevow version, nothing is missing and it
works fine so far but at more than twice the speed that nevow rendered
it (2 sec vs 5 sec). If there are problems with web2 I'll let you know.
Pratically now all time is spent in the db... this is really good, the
nevow performance bottlenecks I reported in the past are all gone.

I'll wait quite a bit before moving CPUShare to Cheetah+web2 (CPUShare
web side is just the boring part, it's not really important as long as I
know I've a safe mid term exit path with web2+Cheetah that I'm currently
exercising with klive), but I'll move it too as soon as I feel web2 sort
of stabilized (breakages that don't require full rewrites will always be
fine of course).

Could somebody suggest the best way to solve the reverse proxy sanely in

Notably we must allow the remote IP to be passed through the proxy (the
autorewrite class already contemplated it but it was never implemented).
Currently I'm sending it down with a string prefix like
"/vhost/http/klive.cpushare.com/xx.xx.xx.xx:yyy/". I guess getRemoteHost
need changes for this, my one is a true hack....

Also please keep the web2 related discussions CC'ed to the list, not
everyone has time to spend on IRC.

If you want to see how to replace nevow with Cheetah for the templates
and how to plug on top of web2 (svn trunk), you can compare the diff
between klive-0.17.tar.bz2 and klive-0.18.tar.bz2. Cheetah is a breath
of fresh air and web2 looks nice too. BTW, stuff like the below could
really go in shared code.

class AccessLoggingObserver(log.BaseCommonAccessLoggingObserver):
	def __init__(self, logpath):
		self.f = file(logpath, 'a')

	def logMessage(self, message):
		self.f.write(message + '\n')


More information about the Twisted-web mailing list