For a super-basic http twisted front-end.
How can I make sure no html is ever written back unless I tell it to.
So, I have my /zoo url below.
For any tracebacks, or ‘No Such Resource’ responses, I want to just drop the connection or return an empty response.
I guess it’s a super-simple one but can’t figure it out 🙂
I know I could do it byt not having my specific Child path, but want to do it efficient, would just want to drop it as early as possible.. Maybe not use Resource?
class HttpApi(resource.Resource):
isLeaf = True
def render_POST(self, request):
return "post..."
application = service.Application("serv")
json_api = resource.Resource()
json_api.putChild("zoo", HttpApi())
web_site = server.Site(json_api)
internet.TCPServer(8001, web_site).setServiceParent(application)
The way twisted.web works is
There is class called Site which is a HTTP factory.
This is called for every request. In fact, a function called getResourceFor is called to obtain the appropriate resource that will serve this request.
This Site class is initialized with root resource. And the function Site.getResourceFor calls resource.getChildForRequest on the root resource
Call flow is:
Now it is time to look at getChildForRequest:
What happens is as resources are registered with putChild(path), they become child resources of that resource.
An example:
Some reflections:
But
You will need to subclass Site to
You will have to create your own resource