开发者

Need help writing a twisted proxy

开发者 https://www.devze.com 2023-03-15 11:33 出处:网络
I want to write a simple proxy that shuffles the text in the body of the requested pages. I have read parts of the twisted documentation and some other similar questions here on stackoverflow but I\'m

I want to write a simple proxy that shuffles the text in the body of the requested pages. I have read parts of the twisted documentation and some other similar questions here on stackoverflow but I'm a bit of a noob so I still don't get it.

This is what I have now, I don't know how to access and modify the page

from twisted.web import proxy, http
from twisted.internet import protocol, reactor
from twisted.开发者_如何转开发python import log
import sys

log.startLogging(sys.stdout)

class ProxyProtocol(http.HTTPChannel):
   requestFactory = PageHandler

class ProxyFactory(http.HTTPFactory):
   protocol = ProxyProtocol

if __name__ == '__main__':
   reactor.listenTCP(8080, ProxyFactory())
   reactor.run()

Can you please help me out? I'd appreciate a simple example (e.g. add something to the body etc...).


What I do is to implement a new ProxyClient, where I modify the data after I've downloaded it from the webserver, and before I send it off to the web browser.

from twisted.web import proxy, http
class MyProxyClient(proxy.ProxyClient):
 def __init__(self,*args,**kwargs):
  self.buffer = ""
  proxy.ProxyClient.__init__(self,*args,**kwargs)
 def handleResponsePart(self, buffer):
  # Here you will get the data retrieved from the web server
  # In this example, we will buffer the page while we shuffle it.
  self.buffer = buffer + self.buffer
 def handleResponseEnd(self):
  if not self._finished:
   # We might have increased or decreased the page size. Since we have not written
   # to the client yet, we can still modify the headers.
   self.father.responseHeaders.setRawHeaders("content-length", [len(self.buffer)])
   self.father.write(self.buffer)
  proxy.ProxyClient.handleResponseEnd(self)

class MyProxyClientFactory(proxy.ProxyClientFactory):
 protocol = MyProxyClient

class ProxyRequest(proxy.ProxyRequest):
 protocols = {'http': MyProxyClientFactory}
 ports = {'http': 80 }
 def process(self):
  proxy.ProxyRequest.process(self)

class MyProxy(http.HTTPChannel):
 requestFactory = ProxyRequest

class ProxyFactory(http.HTTPFactory):
 protocol = MyProxy

Hope this also works for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消