1

我不是 python 扭曲的专家,请帮我解决我的问题,当我尝试路径时 getChild 没有调用localhost:8888/dynamicchild。甚至将 isLeaf 作为 False 在我的资源中。

根据我的理解,当我尝试localhost:8888它应该返回蓝页当然它正在发生但是当我尝试localhost:8888/somex语句打印“getChild called”应该打印在屏幕上但现在它没有发生

from twisted.web import resource 

from twisted.web import server 
from twisted.internet import reactor

class MyResource(resource.Resource):
    isLeaf=False
    def render(self,req):
        return "<body bgcolor='#00aacc' />"

    def getChild(self,path,request):
        print " getChild called "


r=resource.Resource()
r.putChild('',MyResource())
f=server.Site(r)
reactor.listenTCP(8888,f)
reactor.run()
4

1 回答 1

2

那是因为它是您发送到 server.Site 构造函数的根资源,它的 getChild 方法被调用。

尝试这样的事情:

from twisted.web import resource 

from twisted.web import server 
from twisted.internet import reactor

class MyResource(resource.Resource):
    isLeaf=False

    def __init__(self, color='blue'):
        resource.Resource.__init__(self)
        self.color = color

    def render(self,req):
        return "<body bgcolor='%s' />" % self.color

    def getChild(self,path,request):
        return MyResource('blue' if path == '' else 'red')

f=server.Site(MyResource())
reactor.listenTCP(8888,f)
reactor.run()

请求“/”现在将返回蓝色背景,而其他所有内容都返回“红色”。

于 2013-02-08T11:16:07.020 回答