Twisted url action routing

只谈情不闲聊 提交于 2019-12-12 03:37:36

问题


If I have for example this simple TCP server:

from twisted.internet import reactor
from twisted.web.resource import Resource
from twisted.web.server import Site

from resources import SomeResource


logging.info("Starting server...")
root = Resource()
root.putChild("test", SomeResource())
reactor.listenTCP(8080, Site(root))
reactor.run()

With SomeResource which has the render_GET and render_POST methods for example. Then I know I can just send a POST/GET to hostname:8080/test

But now I want to make it more complicated, I would like to do something like hostname:8080/test/status

Could that be defined inside SomeResource() as a method? or do I have to define a new resource for every different url?


回答1:


If you want everything that goes to /test/.... to get to the render (render_GET/render_POST) method of SomeResource, just define it as a leaf:

class SomeResource(Resource):
    isLeaf = True

If you want to look at the part after "test/", request.postpath will include that.



来源:https://stackoverflow.com/questions/37676872/twisted-url-action-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!