2009-08-09 5 views
2

urllib shttpリクエストで返されたメッセージをどのように見ることができますか?それは単純なhttpだった場合私はちょうどソケットのトラフィックを見るだろうが、もちろんhttpsのために動作しません。これを行うために設定できるデバッグフラグはありますか?python urllib、メッセージを見る方法?

import urllib 
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) 
f = urllib.urlopen("https://example.com/cgi-bin/query", params) 

答えて

1

いいえ、これを監視するデバッグフラグはありません。

お気に入りのデバッガを使用できます。それは最も簡単なオプションです。単に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) 
2

あなたは常に

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) 
をmokeypatchingの少しを行うことができます

は出力を与える

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 
関連する問題