需要帮助编写一个扭曲代理

9 投票
1 回答
2945 浏览
提问于 2025-04-16 20:22

我想写一个简单的代理程序,能够随机打乱请求页面的正文内容。我看过一些Twisted的文档和StackOverflow上类似的问题,但我还是有点菜,所以还是不太明白。

这是我现在的代码,我不知道怎么去访问和修改页面。

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

你能帮我一下吗?我会很感激一个简单的例子(比如在正文里加点东西等等)。

1 个回答

6

我做的事情是实现一个新的代理客户端。在我从网络服务器下载数据之后,发送到网页浏览器之前,我会对这些数据进行修改。

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

希望这对你也有帮助。

撰写回答