Python twisted代理发送2个请求

时间:2014-01-30 19:16:54

标签: python http proxy twisted

如何使用此代码才能发送2个单独的请求。请求将按此顺序排列:

Request1:

HEAD http://google.com
Host: google.com

...等待谷歌服务器的回复......

Request2:

GET http://yahoo.com HTTP/1.1
User-Agent: mozilla
Accept: */*

...第一个请求对所有请求都是静态的,从浏览器发送的第二个请求......

我要修改的代码是:


from twisted.web import proxy, http

class SnifferProxy(proxy.Proxy):
    def allContentReceived(self):
        print "Received data..."
        print "method = %s" % self._command
        print "action = %s" % self._path
        print "ended content manipulation\n\n"
        return proxy.Proxy.allContentReceived(self)

class ProxyFactory(http.HTTPFactory):
    protocol = SnifferProxy

if __name__ == "__main__":
    from twisted.internet import reactor
    reactor.listenTCP(8080, ProxyFactory())
    reactor.run()         

扭曲的代理将连接到另一个外部代理 任何帮助表示赞赏..

1 个答案:

答案 0 :(得分:0)

我认为您可以使用callbackProxy.allContentReceived方法的调用作为Agent添加到HEAD请求,从而获得您想要的内容。

from twisted.internet import reactor from twisted.web import proxy, http
from twisted.web.client import Agent
from twisted.web.http_headers import Headers

agent = Agent(reactor)

class SnifferProxy(proxy.Proxy):

    def allContentReceived(self):

        def cbHead(result):
            print "got response for HEAD"

        def doProxiedRequest(result):
            proxy.Proxy.allContentReceived(self)

         # I assumed self._path, but it looks OP wants to do the 
         # HEAD request to the same path always

         PATH = "http://foo.bar"  
         d = agent.request(
             'HEAD', PATH, Headers({'User-Agent': ['twisted']}), None)

         d.addCallback(cbHead)
         d.addCallback(doProxiedRequest)