Ok, I've read the entire Twisted O'Reilly book and gone through many of the docs and tutorials but I still need a bit of direction. I'm trying to make a simple web-based proxy. Similar to the proxy in the Twisted book, but with a web interface. So I could go to http://asdf.com.MYPROXYSITE.com and see the contents of asdf.com. I have the following code that works well as a browser proxy, but I need to figure out how make it web based. My thoughts are that I need to make an HTTP server and then adjust the address in the protocol before finishing the proxy request. Am I on the right track? Does anyone have examples of this? Should I ditch the Proxy classes all together? Thanks for any input. Dave Fowler from twisted.web import proxy, http from twisted.python import log import sys log.startLogging(sys.stdout) class Proxy(proxy.Proxy): def __init__(self, *args): print "inside Proxy" proxy.Proxy.__init__(self, *args) print "Proxy: ", self.__dict__ print dir(self) class ProxyFactory(http.HTTPFactory): # Receive connections from the client """ Receives connections from the client """ def __init__(self): # This function only needed for printing input and stuff print "inside proxy factory" http.HTTPFactory.__init__(self) print self.__dict__ print dir(self) def buildProtocol(self, addr): protocol = Proxy() print "making protocol for addr", addr print "protocol:", protocol.__dict__ return protocol if __name__ == "__main__": PROXY_PORT = 8001 from twisted.internet import reactor reactor.listenTCP(PROXY_PORT, ProxyFactory()) reactor.run()
On Mon, 20 Jul 2009 12:01:22 -0500, dave fowler <davefowler@gmail.com> wrote:
Ok, I've read the entire Twisted O'Reilly book and gone through many of the docs and tutorials but I still need a bit of direction.
I'm trying to make a simple web-based proxy. Similar to the proxy in the Twisted book, but with a web interface. So I could go to http://asdf.com.MYPROXYSITE.com and see the contents of asdf.com.
I have the following code that works well as a browser proxy, but I need to figure out how make it web based.
My thoughts are that I need to make an HTTP server and then adjust the address in the protocol before finishing the proxy request. Am I on the right track? Does anyone have examples of this?
The HTTP proxy implemented in twisted.web.proxy is probably a better starting point. Or, it's at least a good example of how to get a basic proxy together. :) I think you're on the right track, but if you don't want to have to implement this all yourself, you probably don't have to. Jean-Paul
Thanks for your help Jean-Paul. In my example I've just subclassed the protocol and factory so I could print out the properties and have a little control over things as I learn. The proxy could of course be much easily run as from twisted.web import http f = http.HTTPFactory() f.protocol = Proxy I should be a bit more specific about what I'm currently confused about. *I'm trying to find where I can grab the request URI from the client so I can modify it from www.asdf.com.MYPROXYSITE.com to www.asdf.com before the proxy client makes its requests. Where can you access and modify those parameters?* Dave On Mon, Jul 20, 2009 at 1:41 PM, Jean-Paul Calderone <exarkun@divmod.com>wrote:
Ok, I've read the entire Twisted O'Reilly book and gone through many of
On Mon, 20 Jul 2009 12:01:22 -0500, dave fowler <davefowler@gmail.com> wrote: the
docs and tutorials but I still need a bit of direction.
I'm trying to make a simple web-based proxy. Similar to the proxy in the Twisted book, but with a web interface. So I could go to http://asdf.com.MYPROXYSITE.com and see the contents of asdf.com.
I have the following code that works well as a browser proxy, but I need to figure out how make it web based.
My thoughts are that I need to make an HTTP server and then adjust the address in the protocol before finishing the proxy request. Am I on the right track? Does anyone have examples of this?
The HTTP proxy implemented in twisted.web.proxy is probably a better starting point. Or, it's at least a good example of how to get a basic proxy together. :)
I think you're on the right track, but if you don't want to have to implement this all yourself, you probably don't have to.
Jean-Paul
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
On Tue, 21 Jul 2009 11:44:43 -0500, dave fowler <davefowler@gmail.com> wrote:
Thanks for your help Jean-Paul.
In my example I've just subclassed the protocol and factory so I could print out the properties and have a little control over things as I learn. The proxy could of course be much easily run as
from twisted.web import http f = http.HTTPFactory() f.protocol = Proxy
I should be a bit more specific about what I'm currently confused about.
*I'm trying to find where I can grab the request URI from the client so I can modify it from www.asdf.com.MYPROXYSITE.com to www.asdf.com before the proxy client makes its requests. Where can you access and modify those parameters?*
Proxy uses twisted.web.proxy.ProxyRequest as its request factory. The request will have its "process" method called when it has been completely received. You can subclass ProxyRequest, set your proxy to use that subclass as its request factory, and in that subclass, override process to change the uri before calling the base implementation. How's that? Jean-Paul
Perfect! Thanks a ton! That was just the lead I needed. Did some more digging around and now I've got it running great. Next I just have to figure out SSL support :). Thanks again! Dave On Tue, Jul 21, 2009 at 12:19 PM, Jean-Paul Calderone <exarkun@divmod.com>wrote:
Thanks for your help Jean-Paul.
In my example I've just subclassed the protocol and factory so I could
out the properties and have a little control over things as I learn. The proxy could of course be much easily run as
from twisted.web import http f = http.HTTPFactory() f.protocol = Proxy
I should be a bit more specific about what I'm currently confused about.
*I'm trying to find where I can grab the request URI from the client so I can modify it from www.asdf.com.MYPROXYSITE.com to www.asdf.com before
On Tue, 21 Jul 2009 11:44:43 -0500, dave fowler <davefowler@gmail.com> wrote: print the
proxy client makes its requests. Where can you access and modify those parameters?*
Proxy uses twisted.web.proxy.ProxyRequest as its request factory. The request will have its "process" method called when it has been completely received. You can subclass ProxyRequest, set your proxy to use that subclass as its request factory, and in that subclass, override process to change the uri before calling the base implementation. How's that?
Jean-Paul
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
participants (2)
-
dave fowler
-
Jean-Paul Calderone