Simple proxy requiring another proxy with twisted

2019-09-19 17:15发布

问题:

I came across this simple proxy example that uses Twisted:

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

from twisted.internet import reactor
from twisted.web import proxy, server

site = server.Site(proxy.ReverseProxyResource('yahoo.com', 80, ''))
reactor.listenTCP(8080, site)
reactor.run()

The issue is that my computer needs to use a proxy itself to access the web, so I was wondering if there is a way to specify those proxy settings?

回答1:

Not really. At least, there's not an easy way to do it with Twisted.

ReverseProxyResource delegates to ReverseProxyRequest which does a direct TCP connection to the specified host (in this case, yahoo.com).

You could probably cobble something together by adapting ReverseProxyRequest to use twisted.web.client.ProxyAgent. Arguably, Twisted itself should provide a way to parametrize ReverseProxyResource by agent.