我怎么能看被发送的消息回来,对urllib的S-HTTP请求?如果是简单的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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top