簡體   English   中英

Python扭曲的代理發送2個請求

[英]Python twisted proxy to send 2 requests

我如何處理此代碼,以便能夠發送2個單獨的請求。 請求將按以下順序進行:

請求1:

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

...等待來自Google服務器的回復...

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

扭曲的代理將連接到另一個外部代理。不勝感激。

我認為您可以通過將對Proxy.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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM