собственно как сделать веб-клиента, который бы лазил через прокси по тру-твистедовски?
курение гугля просветления не принесло
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from twisted.internet import reactor
from twisted.python import log
from twisted.web.http import HTTPFactory
from twisted.web.proxy import Proxy
if __name__ == '__main__':
fileObserver = log.FileLogObserver(sys.stdout)
fileObserver.timeFormat = '%H:%M:%S'
log.startLoggingWithObserver(fileObserver.emit)
factory = HTTPFactory()
factory.protocol = Proxy
reactor.listenTCP(8080, factory)
reactor.run()
from urllib import urlencode
from StringIO import StringIO
from twisted.web2 import stream as stream_mod, http_headers
from twisted.web2.client.http import HTTPClientProtocol, ClientRequest
from twisted.internet import reactor, protocol, defer
def err(reason, defered):
if not defered.called:
defered.errback(reason)
def geturl_proxy(host, path, proxy_host, proxy_port, **kwargs):
d = protocol.ClientCreator(reactor, HTTPClientProtocol).connectTCP(proxy_host, proxy_port)
path = 'http://%s%s' % (host, path)
ret = defer.Deferred()
def gotResp(resp, defered):
def done(n, res, defered):
defered.callback(res.getvalue())
res = StringIO()
stream_mod.readStream(resp.stream, res.write).addCallback(done, res, defered).addErrback(err, defered)
def sendReq(proto):
q = '?'+urlencode(kwargs) if kwargs else ''
headers = http_headers.Headers({'Host': host})
proto.submitRequest(ClientRequest("GET", path+q, headers, None)).addCallback(gotResp, ret).addErrback(err, ret)
d.addCallback(sendReq).addErrback(err, ret)
return ret
if __name__=="__main__":
def f(*args):
for arg in args: print arg
reactor.stop()
geturl_proxy('ya.ru', '/', 'proxy.zzz', 3128).addBoth(f)
reactor.run()