为什么扭曲资源。资源执行render()两次?

2024-04-26 07:50:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的。为什么要打印“render()”两次?我知道如果我回来服务器尚未完成,它只打印一次,但我希望返回string/JSON。有什么帮助吗?在

代码:

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()

输出:

^{pr2}$

Tags: fromimportself服务器stringserverrequestdef
1条回答
网友
1楼 · 发布于 2024-04-26 07:50:27

因为您的web浏览器正在请求favicon.ico。如果您在render方法中打印request.postpath,您将看到只有一个请求正在命中您期望的页面。在

相关问题 更多 >