2014-01-30 96 views
1

我該如何處理這段代碼才能發送2個不同的請求。該請求是按以下順序:蟒蛇扭曲代理髮送2個請求

Request1:

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

...等待從谷歌服務器的答覆...

請求2:

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

扭曲的代理將連接到任何幫助表示讚賞另一個外部代理 ..

回答

0

我想你可以通過添加得到你想要的使用AgentProxy.allContentReceived方法作爲callback調用至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) 
+0

這是來自wireshark的圖片,顯示了我試圖實現的內容![請求] [1] [1] http://imgur.com/AR3hYPj @bennomadic – mikie