Need help writing a twisted proxy

爷,独闯天下 提交于 2019-12-20 12:38:45

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/6491932/need-help-writing-a-twisted-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!