如何让我的简单扭曲代理工作?
我正在尝试使用 Twisted.Web 框架。
请注意三行注释(#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 方法。
我不知道我做错了什么?任何帮助将不胜感激。
I am attempting to make use of the Twisted.Web framework.
Notice the three line comments (#line1, #line2, #line3). I want to create a proxy (gateway?) that will forward a request to one of two servers depending on the url. If I uncomment either comment 1 or 2 (and comment the rest), the request is proxied to the correct server. However, of course, it does not pick the server based on the 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()
As the code above currently stands, when I run this script and navigate to server "localhost:8080/ANYTHING_AT_ALL" I get the following response.
Method Not Allowed
Your browser approached me (at /ANYTHING_AT_ALL) with the method "GET". I
only allow the methods GET, POST here.
I don't know what I am doing wrong? Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您的
Simple
类实现了getChild()
方法,因此暗示这不是叶节点,但是,您通过设置 <代码>isLeaf = True。 (叶节点如何有子节点?)。尝试将
isLeaf = True
更改为isLeaf = False
,您会发现它按照您的预期重定向到代理。来自
Resource.getChild
文档字符串:Since your
Simple
class implements thegetChild()
method, it is implied that this is not a leaf node, however, you are stating that it is a leaf node by settingisLeaf = True
. (How can a leaf node have a child?).Try changing
isLeaf = True
toisLeaf = False
and you'll find that it redirects to the proxy as you'd expect.From the
Resource.getChild
docstring:这是最终的工作解决方案。基本上有两个资源请求发送到 GAE 服务器,其余所有请求发送到 GWT 服务器。
除了实现 mhawke 的更改之外,只有一项更改,那就是将“/”+名称添加到代理服务器路径。我认为必须这样做,因为路径的该部分已被消耗并放置在“名称”变量中。
谢谢。
Here is the final working solution. Basically two resource request go to the GAE server, and all remaining request go to the GWT server.
Other than implementing mhawke's change, there is only one other change, and that was adding '"/" + name' to the proxy servers path. I assume this had to be done because that portion of the path was consumed and placed in the 'name' variable.
Thank you.