如何让我的简单扭曲代理工作? [英] How do I get my simple twisted proxy to work?

查看:39
本文介绍了如何让我的简单扭曲代理工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Twisted.Web 框架.

I am attempting to make use of the Twisted.Web framework.

注意三行注释(#line1、#line2、#line3).我想创建一个代理(网关?),它将根据 url 将请求转发到两个服务器之一.如果我取消注释 1 或 2(并注释其余部分),请求将被代理到正确的服务器.但是,当然,它不会根据 URL 选择服务器.

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()

正如上面的代码,当我运行这个脚本并导航到服务器localhost:8080/ANYTHING_AT_ALL"时,我得到以下响应.

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.

不允许的方法

您的浏览器通过GET"方法找到了我(在/ANYTHING_AT_ALL).一世此处只允许 GET、POST 方法.

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.

推荐答案

由于您的 Simple 类实现了 getChild() 方法,因此暗示这不是一个叶节点,但是,您通过设置 isLeaf = True 声明它是叶节点.(叶子节点怎么会有子节点?).

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?).

尝试将 isLeaf = True 更改为 isLeaf = False,您会发现它会按照您的预期重定向到代理.

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

来自 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....

这篇关于如何让我的简单扭曲代理工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆