2014-01-30 3 views
1

어떻게이 코드를 사용하여 2 개의 개별 요청을 보낼 수 있습니까? 두 번째 ...파이썬 트위스트 프록시가 2 개의 요청을 전송합니다.

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

: ...

Request2 ...

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

구글 서버에서 응답을 기다립니다

Request1 : 요청은 순서에있을 것입니다 첫 번째 요청이 모든 요청에 ​​대해 정적 인 동안 브라우저에서 전송 된 요청 ...

코드 수정하려고하는 것입니다 :


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

나는 당신이를 추가하여 당신이 원하는 것을 얻을 수 있다고 생각 Agent을 사용하여 HEAD 요청에 callback으로 Proxy.allContentReceived 메서드를 호출합니다.

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

관련 문제