Pythonのurllibは、どのようにメッセージを監視するには?
質問
どのように私はメッセージがurllibはのSHTTP要求に背中とのために送られて見ることができますか?それは単純なHTTPた場合、私はちょうどソケットトラフィックを監視しますが、HTTPSは動作しませんもちろんのでしょう。私はこれを行います設定することができ、デバッグフラグがありますか?
import urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
f = urllib.urlopen("https://example.com/cgi-bin/query", params)
解決
いいえ、これを見て何のデバッグフラグがありません。
あなたの好きなデバッガを使用することができます。これは、最も簡単なオプションです。ただ、urlopen関数にブレークポイントを追加し、あなたが行われています。
別のオプションは、あなた自身のダウンロード機能を書くことになります:
def graburl(url, **params):
print "LOG: Going to %s with %r" % (url, params)
params = urllib.urlencode(params)
return urllib.urlopen(url, params)
そして、このようにそれを使用します:
f = graburl("https://example.com/cgi-bin/query", spam=1, eggs=2, bacon=0)
他のヒント
あなたはいつもmokeypatchingの少しを行うことができます。
import httplib
# override the HTTPS request class
class DebugHTTPS(httplib.HTTPS):
real_putheader = httplib.HTTPS.putheader
def putheader(self, *args, **kwargs):
print 'putheader(%s,%s)' % (args, kwargs)
result = self.real_putheader(self, *args, **kwargs)
return result
httplib.HTTPS = DebugHTTPS
# set a new default urlopener
import urllib
class DebugOpener(urllib.FancyURLopener):
def open(self, *args, **kwargs):
result = urllib.FancyURLopener.open(self, *args, **kwargs)
print 'response:'
print result.headers
return result
urllib._urlopener = DebugOpener()
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
f = urllib.urlopen("https://www.google.com/", params)
出力を提供します。
putheader(('Content-Type', 'application/x-www-form-urlencoded'),{})
putheader(('Content-Length', '21'),{})
putheader(('Host', 'www.google.com'),{})
putheader(('User-Agent', 'Python-urllib/1.17'),{})
response:
Content-Type: text/html; charset=UTF-8
Content-Length: 1363
Date: Sun, 09 Aug 2009 12:49:59 GMT
Server: GFE/2.0
所属していません StackOverflow