我正在尝试利用扭曲网框架。
请注意三行注释(#line1、#line2、#line3)。我想创建一个代理(网关?),它将根据 url 将请求转发到两个服务器之一。如果我取消注释 1 或 2(并注释其余部分),请求将被代理到正确的服务器。但是,当然,它不会根据 URL 选择服务器。
from twisted.internet import reactor
from twisted.web import proxy, server
from twisted.web.resource import Resource
class Simple(Resource):
isLeaf = True
allowedMethods = ("GET","POST")
def getChild(self, name, request):
if name == "/" or name == "":
return proxy.ReverseProxyResource('localhost', 8086, '')
else:
return proxy.ReverseProxyResource('localhost', 8085, '')
simple = Simple()
# site = server.Site(proxy.ReverseProxyResource('localhost', 8085, '')) #line1
# site = server.Site(proxy.ReverseProxyResource('localhost', 8085, '')) #line2
site = server.Site(simple) #line3
reactor.listenTCP(8080, site)
reactor.run()
正如上面的代码当前所示,当我运行此脚本并导航到服务器“localhost:8080/ANYTHING_AT_ALL”时,我得到以下响应。
不允许的方法
您的浏览器使用“GET”方法接近我(在/ANYTHING_AT_ALL)。我
这里只允许使用 GET、POST 方法。
我不知道我做错了什么?任何帮助,将不胜感激。
自从你的Simple
类实现了getChild()
方法,这意味着这不是叶节点,但是,您通过设置来声明它是叶节点isLeaf = True
。 (叶节点如何有子节点?)。
尝试改变isLeaf = True
to isLeaf = False
您会发现它按照您的预期重定向到代理。
来自Resource.getChild
文档字符串:
... This will not be called if the class-level variable 'isLeaf' is set in
your subclass; instead, the 'postpath' attribute of the request will be
left as a list of the remaining path elements....
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)