2011-11-09 7 views
3

私はPython 2.6.5を使用していますが、HTTP経由で送信された生のHTTPリクエストをキャプチャしようとしています。次の:urllib2を介してPython生のhttpリクエストの取得に関する問題

  • HTTPおよびHTTPSリクエストがプロキシハンドラせずに正常に動作:生のHTTPリクエストが
  • HTTPリクエストがプロキシハンドラで正常に動作キャプチャ:プロキシ[OK]を、生のHTTPリクエストは
  • HTTPS要求がで失敗キャプチャプロキシハンドラ:プロキシは正常ですが、生のHTTPリクエストはキャプチャされません!

次の質問は近いですが、私の問題が解決しない:

これは私がやっているものです:

class MyHTTPConnection(httplib.HTTPConnection): 
    def send(self, s): 
      global RawRequest 
      RawRequest = s # Saving to global variable for Requester class to see 
      httplib.HTTPConnection.send(self, s) 

class MyHTTPHandler(urllib2.HTTPHandler): 
    def http_open(self, req): 
      return self.do_open(MyHTTPConnection, req) 

class MyHTTPSConnection(httplib.HTTPSConnection): 
    def send(self, s): 
      global RawRequest 
      RawRequest = s # Saving to global variable for Requester class to see 
      httplib.HTTPSConnection.send(self, s) 

class MyHTTPSHandler(urllib2.HTTPSHandler): 
    def https_open(self, req): 
      return self.do_open(MyHTTPSConnection, req) 

リクエスタクラス:

global RawRequest 
ProxyConf = { 'http':'http://127.0.0.1:8080', 'https':'http://127.0.0.1:8080' } 
# If ProxyConf = { 'http':'http://127.0.0.1:8080' }, then Raw HTTPS request captured BUT the proxy does not see the HTTPS request! 
# Also tried with similar results:  ProxyConf = { 'http':'http://127.0.0.1:8080', 'https':'https://127.0.0.1:8080' } 
ProxyHandler = urllib2.ProxyHandler(ProxyConf) 
urllib2.install_opener(urllib2.build_opener(ProxyHandler, MyHTTPHandler, MyHTTPSHandler)) 
urllib2.Request('http://www.google.com', None) # global RawRequest updated 
# This is the problem: global RawRequest NOT updated!? 
urllib2.Request('https://accounts.google.com', None) 

しかし、私はにproxyHandlerを削除した場合、それは動作します!:

私が追加することができますどのように
global RawRequest 
urllib2.install_opener(urllib2.build_opener(MyHTTPHandler, MyHTTPSHandler)) 
urllib2.Request('http://www.google.com', None) # global RawRequest updated 
urllib2.Request('https://accounts.google.com', None) # global RawRequest updated 

をRawRequestへのアクセスを維持しながら、ProxyHandlerを組み合わせて使用​​しますか?

ありがとうございます。

+0

回答があることが確かであれば、コメントではなく回答として投稿してください。 –

+0

良い点ジョナサン、ちょうど答えのセクションにコメントを移動しました。乾杯。 –

答えて

1

自分の質問に答える:RawRequestに問題を解決させる、基本的なライブラリのバグのようです:HTTP Raw要求が最初の項目です。カスタムHTTPSクラスは何度か呼び出され、最後は空白です。以前の初期化と

RawRequest.append(s) 

:ちょうどに変更する必要がありますカスタムHTTPクラスは一度だけ呼び出されるという事実は、これはPythonでのバグであることを示唆しているが、リストのソリューションは

その周り
RawRequest = s 

を取得しますRawRequest = []と生のリクエストの取得RawRequest[0](リストの最初の要素)

関連する問題