0

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?

lapinkoira
  • 8,320
  • 9
  • 51
  • 94
  • http://twisted.readthedocs.io/en/twisted-16.2.0/web/howto/web-in-60/dynamic-dispatch.html should help you out. If your code gets really complicated, I would recommend Klein https://github.com/twisted/klein which is much simpler to write. – notorious.no Jun 08 '16 at 17:05

1 Answers1

2

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.

moshez
  • 36,202
  • 1
  • 22
  • 14