Need help writing a twisted proxy

前端 未结 1 1017
失恋的感觉
失恋的感觉 2021-02-06 04:08

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

相关标签:
1条回答
  • 2021-02-06 04:35

    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 讨论(0)
提交回复
热议问题