2012-03-27 97 views
1

我新來扭曲。爲什麼打印「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() 
+0

我使用2.6.6和扭曲的10.2.0進行了測試。它按預期工作(打印一次) – 2012-03-27 05:06:42

回答

2

因爲你的網頁瀏覽器所請求favicon.ico。如果您在render方法中打印request.postpath,則會看到只有其中一個請求觸及您期望的頁面。

+0

感謝:)你是正確的關於favicon.ico – 2012-03-27 05:06:02

+0

@MickeyCheong你正在使用哪個瀏覽器?我正在使用Firefox – 2012-03-27 05:28:24

相關問題