如何让我的简单扭曲代理工作?

发布于 2024-08-22 09:10:51 字数 1237 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-08-29 09:10:51

由于您的 Simple 类实现了 getChild() 方法,因此暗示这不是叶节点,但是,您通过设置 <代码>isLeaf = True。 (叶节点如何有子节点?)。

尝试将 isLeaf = True 更改为 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....

Since your Simple class implements the getChild() method, it is implied that this is not a leaf node, however, you are stating that it is a leaf node by setting isLeaf = True. (How can a leaf node have a child?).

Try changing isLeaf = True to isLeaf = False and you'll find that it redirects to the proxy as you'd expect.

From the Resource.getChild docstring:

... 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....
温馨耳语 2024-08-29 09:10:51

这是最终的工作解决方案。基本上有两个资源请求发送到 GAE 服务器,其余所有请求发送到 GWT 服务器。

除了实现 mhawke 的更改之外,只有一项更改,那就是将“/”+名称添加到代理服务器路径。我认为必须这样做,因为路径的该部分已被消耗并放置在“名称”变量中。

from twisted.internet import reactor
from twisted.web import proxy, server
from twisted.web.resource import Resource

class Simple(Resource):
    isLeaf = False
    allowedMethods = ("GET","POST")
    def getChild(self, name, request):
        print "getChild called with name:'%s'" % name
        if name == "get.json" or name == "post.json":
            print "proxy on GAE"
            return proxy.ReverseProxyResource('localhost', 8085, "/"+name)
        else:
            print "proxy on GWT"
            return proxy.ReverseProxyResource('localhost', 8086, "/"+name)

simple = Simple()
site = server.Site(simple)
reactor.listenTCP(8080, site)
reactor.run()

谢谢。

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.

from twisted.internet import reactor
from twisted.web import proxy, server
from twisted.web.resource import Resource

class Simple(Resource):
    isLeaf = False
    allowedMethods = ("GET","POST")
    def getChild(self, name, request):
        print "getChild called with name:'%s'" % name
        if name == "get.json" or name == "post.json":
            print "proxy on GAE"
            return proxy.ReverseProxyResource('localhost', 8085, "/"+name)
        else:
            print "proxy on GWT"
            return proxy.ReverseProxyResource('localhost', 8086, "/"+name)

simple = Simple()
site = server.Site(simple)
reactor.listenTCP(8080, site)
reactor.run()

Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文