3
私はPython 2.6.5を使用していますが、HTTP経由で送信された生のHTTPリクエストをキャプチャしようとしています。次の:urllib2を介してPython生のhttpリクエストの取得に関する問題
- HTTPおよびHTTPSリクエストがプロキシハンドラせずに正常に動作:生のHTTPリクエストが
- HTTPリクエストがプロキシハンドラで正常に動作キャプチャ:プロキシ[OK]を、生のHTTPリクエストは
- HTTPS要求がで失敗キャプチャプロキシハンドラ:プロキシは正常ですが、生のHTTPリクエストはキャプチャされません!
次の質問は近いですが、私の問題が解決しない:
- How do you get default headers in a urllib2 Request? < - 私のソリューションが頻繁にこの
- Python urllib2 > HTTP Proxy > HTTPS request
- This sets the proxy for each request <に基づいている - 仕事とそれをやっていませんでしたオープナーを介して開始時に一度よりエレガントで効率的です(各リクエストのプロキシを設定する代わりに)
これは私がやっているものです:
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を組み合わせて使用しますか?
ありがとうございます。
回答があることが確かであれば、コメントではなく回答として投稿してください。 –
良い点ジョナサン、ちょうど答えのセクションにコメントを移動しました。乾杯。 –