python urllib,如何查看消息?

发布于 2024-07-30 20:59:08 字数 260 浏览 5 评论 0原文

如何查看 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)

How can I watch the messages being sent back and for on urllib shttp requests? If it were simple http I would just watch the socket traffic but of course that won't work for https. Is there a debug flag I can set that will do this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩人痒 2024-08-06 20:59:11

不,没有调试标志来观察这一点。

您可以使用您最喜欢的调试器。 这是最简单的选择。 只需在 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)

No, there's no debug flag to watch this.

You can use your favorite debugger. It is the easiest option. Just add a breakpoint in the urlopen function and you're done.

Another option would be to write your own download function:

def graburl(url, **params):
    print "LOG: Going to %s with %r" % (url, params)
    params = urllib.urlencode(params)
    return urllib.urlopen(url, params)

And use it like this:

f = graburl("https://example.com/cgi-bin/query", spam=1, eggs=2, bacon=0)
幸福不弃 2024-08-06 20:59:10

你总是可以做一些 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

You can always do a little bit of 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)

gives the output

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文