简体   繁体   中英

Python twisted proxy to send 2 requests

How can I work on this code to be able to send 2 separate requests. The requests would be in this order:

Request1 :

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

... wait for reply from google server ...

Request2 :

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

... second request sent from browser while first request is static for all requests ...

The code I'm trying to modify is:


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

The twisted proxy would be connecting to another external proxy Any help is appreciated..

I think you can get what you want by adding the call to the Proxy.allContentReceived method as a callback to a HEAD request using Agent .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM