一个超基本的http扭曲的前端。我如何才能确保不会回写html,除非我告诉它这样做。
所以,我下面有我的/zoo url。对于任何回溯或“没有这样的资源”响应,我只想丢弃连接或返回一个空响应。
我猜这是一个超级简单的问题,但我无法弄清楚:)我知道我可以通过没有特定的子路径来做到这一点,但我想要高效地完成它,只想尽可能早地放弃它。也许不使用资源?
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)发布于 2010-11-30 04:16:20
先介绍一些基础知识
twisted.web的工作方式是
有一个名为Site的类,它是一个HTTP工厂。对于每个请求,都会调用此函数。实际上,调用了一个名为getResourceFor的函数来获取将为该请求提供服务的适当资源。此站点类使用根资源初始化。函数Site.getResourceFor在根资源上调用resource.getChildForRequest
呼叫流是:
Site.getResourceFor -> resource.getChildForRequest (根资源)
现在是时候看看getChildForRequest了:
def getChildForRequest(resource, request):
"""
Traverse resource tree to find who will handle the request.
"""
while request.postpath and not resource.isLeaf:
pathElement = request.postpath.pop(0)
request.prepath.append(pathElement)
resource = resource.getChildWithDefault(pathElement, request)
return resource当资源注册到putChild(路径)时,它们将成为该资源的子资源。举个例子:
root_resource
|
|------------ resource r1 (path = 'help')
|----resource r2 (path = 'login') |
| |----- resource r3 (path = 'registeration')
| |----- resource r4 (path = 'deregistration')一些思考:
服务器请求
但
使用路径http://../help/registration/xxx/
的
解决方案的
:
您需要将Site子类化为
你必须创建你自己的资源
def render(self, request):
request.setResponseCode(...)
return ""https://stackoverflow.com/questions/4307294
复制相似问题