【问题标题】:Python twisted proxy to send 2 requestsPython 扭曲代理发送 2 个请求
【发布时间】:2014-01-30 19:16:54
【问题描述】:

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

请求1:

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

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

【问题讨论】:

    标签: python http proxy twisted


    【解决方案1】:

    我认为您可以通过将Proxy.allContentReceived 方法的调用作为callback 添加到使用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)
    

    【讨论】:

    • 这是一张来自 wireshark 的图片,展示了我想要实现的目标![request][1] [1]imgur.com/AR3hYPj@bennomadic
    猜你喜欢
    • 2022-12-03
    • 2013-02-05
    • 1970-01-01
    • 2013-04-26
    • 2016-02-21
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多