[Twisted-web] Run Nevow and XMLRPC on a single port?

Simon Hedberg simon at hedberg.net
Thu Dec 14 06:13:21 CST 2006

Phil Mayers wrote:
> Marian Schubert wrote:
>>> I believe you can do this:
>>> class Xrpc(xmlrpc.XMLRPC):
>>>    def xmlrpc_echo(self, arg):
>>>      return arg
>>> xrpc = Xrpc()
>>> page = NevowRootPage()
>>> page.putChild('RPC2', xrpc)
>> just make sure xmlrpc resource isn't behind nevow.guard
> It's a shame you can't persuade guard by URL arguments to not try an
> HTTP redirect to HTML login, but issue an HTTP auth challenge.
> Bizarrely nevow guard will accept http auth credentials if they're
> given, but I don't know of a single HTTP client that will submit them
> without getting the 401 first.
> _______________________________________________

You can solve this by modifying locateChild in your root resource and
returning a 401 when needed. 

if not loggedin:
   if childSegments[0] == "RPC2":
      return AuthReq(), ()
   return login.LoginPage(), ()

class AuthReq:
    """A simple 401 (Auth required) page.
    __implements__ = inevow.IResource, 

    notAuth = "<html><head><title>Authentication
required</title><head><body>This resource is protected.</body></html>"
    original = None

    def locateChild(self, ctx, segments):
        return None, ()

    def renderHTTP(self, ctx):
        request = inevow.IRequest(ctx)
        request.setHeader('WWW-Authenticate', 'Basic
realm="rpc at yourhost.com"')
        return self.notAuth

    def __nonzero__(self):
        return False

Hope this helps.

More information about the Twisted-web mailing list