[Twisted-Python] [Tutor] Help on personnal Proxy
![](https://secure.gravatar.com/avatar/8a22c5e765f3a07030458f0a9fad969a.jpg?s=120&d=mm&r=g)
Hi all, I tried to read some parts of the how-to, tried to google around in order to create a simple personnal web proxy with no luck. For now, I don't want to do anything special, but having a proxy on my localhost, watching for my connections. Later, I may want to enhance its functionnality (a la "proxomitron") I thought that twisted may certainly be the perfect tool to do that, and I thought this would be a good way to start using it. I know about the proxy class, but, when I try to use it: (taken from Proxy class docstring): from twisted.protocols import http from twisted.web import proxy from twisted.internet import reactor f=http.HTTPFactory() f.HTTPChannel=proxy.Proxy reactor.listenTCP(8765,f) reactor.run() I then try an 'export http_proxy=http://localhost:8765' and wget http://localhost:8080 (where a nice twisted.web server is waiting) Not working. Even a telnet on port 8765 shown that nothing is responding. I then thougt about proxy.Proxy which perhaps need to be instanciated: f.HTTPChannel=proxy.Proxy() Same punition. Then, I thougth that I certainly need to subclass my proxy to have it do some stuff: class myProx(proxy.Proxy): def connectionMade(self,*args): print "cool...." def dataReceived(self,*args): print args I then again try with myProx *and*รน myProx(). Nothing worked. Well, I search for anything on the mailing list archive, but with no luck. Do I have to say that any help or RTFM would be appreciated ! Thanx in advance, OPQ ----------------------------------------------------------------- Ce message est confidentiel ; son contenu ne represente en aucun cas un engagement de la part de Dexia Credit Local ou de Dexia CLF Banque. Toute publication, utilisation ou diffusion, meme partielle, doit etre autorisee prealablement par l'emetteur. Si vous n'etes pas destinataire de ce message, merci d'en avertir immediatement l'expediteur. This message is confidential ; its contents do not constitute a commitment by Dexia Credit Local or Dexia CLF Banque. Any unauthorised disclosure, use or dissemination, either whole or partial, is prohibited. If you are not the intended recipient of the message, please notify the sender immediately. ----------------------------------------------------------------- Consultez notre site internet www.dexia-clf.fr La cle d'une gestion optimisee du secteur public local -----------------------------------------------------------------
![](https://secure.gravatar.com/avatar/6537220bef05d90f4b8ab6ecaac76e66.jpg?s=120&d=mm&r=g)
"POYEN OP Olivier (DCL)" <Olivier.POYEN@clf-dexia.com> writes:
I tried to read some parts of the how-to, [...] with no luck.
The short answer to your question is to replace: f.HTTPChannel=proxy.Proxy with: f.protocol=proxy.Proxy Or, I've written a (yet another) web proxy that you might want to try too. Using my MinProxy (for Minimal proxy) should be fairly straightforward, simply dump MinProxy.py and HttpHeader.py into the same directory, and then run MinProxy (there's no #!, so you'll have to type python MinProxy.py if you're not on Windows). If you want to look at (or mess with) the pages as they flow through MinProxy, it's a bit different (hopefully simpler) than web.Proxy. Simply subclass MinProxy, and then override: onHeaderFromServer( self, header ): onHeaderFromServer( self, header ): onBodyFromBrowser( self, header, body ): onBodyFromServer( self, header, body ): depending on what you're interested in. If you override them, you'll probably want to call: self.sendLinesToServer( header ) self.sendLinesToBrowser( header ) self.sendBufferToServer( body ) self.sendBufferToBrowser( body ) One other difference is that the headers in the above methods are encapsulated as HTTPHeader objects. When treated as lists, they return the header lines, untouched, in the order received. For example: for line in header: print line will print the lines as they were received. When treated as dictionaries, HTTPHeader objects will return the desired field. For example: header['content-type'] might return 'text/html', and: header.get( 'content-encoding', None ) might return 'gzip' (or None). The keys are all converted to lower-case, the fields are untouched. Anyway, if any of this looks the least bit interesting, please feel free to grab it from: http://nonplussed.eholm.com/twisted Why write another web proxy? Well, after playing with web.Proxy, I decided to simplify life (or, at least my particular situation) by writing a proxy based on basic.LineReceiver instead of http.HTTPClient and http.HTTPChannel. I found that web.Proxy was choking on some mainstream'ish sites such as msn.com and amazon.com. While attempting to patch-up the problems, it occurred to me that I was messing with code in the HTTPxxx classes that was parsing the headers. And while *real* HTTP clients and servers need to grok the headers, my simple proxy application doesn't need such sophistication. So I decided to bypass the hard-core HTTP code, and come-up with a simpler approach (famous last words...). I'd be happy to contribute this to twisted, if anybody's interested. It needs some cleanup, as none of it conforms to the twisted coding standards (e.g. not a single docstring), and there aren't any twisted-style test cases (oh, and the name sucks too...). Hopefully there's enough there to give folks a feel for whether or not it might be useful - if so I'll be happy to clean it up and submit it, if not I won't bother. In the meantime, feel free to give it a try, and send any comments or questions my way. Eric.
participants (2)
-
Eric Holm
-
POYEN OP Olivier (DCL)