Dipro Sen
Dipro Sen

Reputation: 4670

python twisted HTTP Proxy

I am very new to python. I am starting with twisted. As I saw in Twisted documentation, just this can make a proxy server.

class MyProxy(proxy.Proxy):
    pass

class ProxyFactory(http.HTTPFactory):
    protocol = MyProxy

reactor.listenTCP(8080, ProxyFactory())
reactor.run()

ProxyFactory::protocol looks like a member variable ? but MyProxy is a typename and I don't know is it a kind of template ? otherwise how can I assign a type into a variable ?

What I actually want to achive is simply reject requests to certain endpoints. I was thinking of overriding buildProtocol But the supplied argument addr is always 127.0.0.1

Upvotes: 1

Views: 1507

Answers (1)

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83788

ProxyFactory.protocol is a Python class attribute. Because Python is a dynamically typed language it has more flexibility over e.g. Java how variables and classes are defined.

MyProxy is an empty class of Proxy subclass which does not define any behavior over what is supplied by default in proxy.Proxy. What you probably need to do (I am not expert in Twisted internals) is to look into documentation of proxy.Proxy class and override some method functions by redefining them in MyProxy, so that those methods perform the logic you are looking for.

If you look the source code from Twisted API documentation it looks like a good place to add custom logic would be ProxyRequest.procese() where it starts connecting the remote host after parsing incoming HTTP request.

So you (maybe) would need to subclass ProxyRequest class, supply it to your MyProxy via requestFactory class attribute.

Upvotes: 3

Related Questions