How can I make my simple twisted proxy work?

I am trying to use the Twisted.Web framework.

Note the three line comments (# line1, # line2, # line3). I want to create a proxy (gateway?) That will redirect the request to one of the two servers depending on the URL. If I uncomment the comment 1 or 2 (and comment the rest), the request is proxied to the correct server. However, of course, he does not select a 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()

Since the above code is currently standing when I run this script and go to the server "localhost: 8080 / ANYTHING_AT_ALL", I get the following response.

Method not allowed

Your browser approached me (at / ANYTHING_AT_ALL) using the "GET" method. I only allow GET, POST methods.

, ? .

+3
2

Simple getChild(), , node, , node, isLeaf = True. ( node ?).

isLeaf = True isLeaf = False, , -, .

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....
+4

. GAE, GWT.

mhawke, , "/" + " -. , , " name".

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

.

+2

Source: https://habr.com/ru/post/1732926/


All Articles