我新來扭曲。爲什麼打印「render()」兩次?我知道如果我返回server.NOT_DONE_YET,它只會打印一次,但我希望返回字符串/ JSON。任何幫助?爲什麼Twisted resource.Resource執行render()兩次?
代碼:
from twisted.web import resource, server
from twisted.internet import reactor
import simplejson
class WResource(resource.Resource):
isLeaf=True
def __init__(self):
print "resource started"
def render(self, request):
print "render()"
request.setHeader('Content-Type', 'application/json')
return simplejson.dumps(dict(through_port=8080, subdomain='hello'))
reactor.listenTCP(9000, server.Site(WResource()))
reactor.run()
輸出:
> python server.py
resource started
render()
render()
我使用2.6.6和扭曲的10.2.0進行了測試。它按預期工作(打印一次) – 2012-03-27 05:06:42