扭曲的网址操作路由 [英] Twisted url action routing
问题描述
例如,如果我有这个简单的TCP服务器:
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()
使用SomeResource,例如具有render_GET和render_POST方法. 然后我知道我可以发送POST/GET到主机名:8080/test
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
但是现在我想使其更加复杂,我想做类似hostname:8080/test/status的事情
But now I want to make it more complicated, I would like to do something like hostname:8080/test/status
可以在SomeResource()内部将其定义为方法吗?还是必须为每个不同的URL定义新资源?
Could that be defined inside SomeResource() as a method? or do I have to define a new resource for every different url?
推荐答案
如果您希望/test/....的所有内容都进入SomeResource的render(render_GET/render_POST)方法,只需将其定义为叶:
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
如果要查看"test/"之后的部分,request.postpath将包括该部分.
If you want to look at the part after "test/", request.postpath will include that.
这篇关于扭曲的网址操作路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!