【发布时间】:2016-07-27 12:49:32
【问题描述】:
我正在 python 中设置 HTTP 代理来过滤网页内容。我在 StackOverflow 上找到了一个 good example,它正是使用 Twisted 完成的。但是,我需要另一个代理来访问网络。因此,代理需要将请求转发到另一个代理。使用twisted.web.proxy 执行此操作的最佳方法是什么?
我发现 a related question 需要类似的东西,但来自 反向代理。
我最好的猜测是应该可以通过修改或子类化twisted.web.proxy.ProxyClient 来连接下一个代理而不是直接连接到网络来构建链式代理。不幸的是,我在文档中没有找到有关如何执行此操作的任何线索。
我目前拥有的代码 (cite):
from twisted.python import log
from twisted.web import http, proxy
class ProxyClient(proxy.ProxyClient):
def handleResponsePart(self, buffer):
proxy.ProxyClient.handleResponsePart(self, buffer)
class ProxyClientFactory(proxy.ProxyClientFactory):
protocol = ProxyClient
class ProxyRequest(proxy.ProxyRequest):
protocols = dict(http=ProxyClientFactory)
class Proxy(proxy.Proxy):
requestFactory = ProxyRequest
class ProxyFactory(http.HTTPFactory):
protocol = Proxy
portstr = "tcp:8080:interface=localhost" # serve on localhost:8080
if __name__ == '__main__':
import sys
from twisted.internet import endpoints, reactor
log.startLogging(sys.stdout)
endpoint = endpoints.serverFromString(reactor, portstr)
d = endpoint.listen(ProxyFactory())
reactor.run()
【问题讨论】:
标签: python twisted http-proxy