如何使用 urlib2 访问错误的响应标头?

发布于 2024-11-07 12:44:24 字数 513 浏览 3 评论 0原文

我正在使用 Harvest API (http://www.getharvest.com/api)。当客户端超过其配额时,将返回 503 响应。在该响应中应该有一个名为“Retry-After”的标头,告诉我在重试之前需要等待多长时间。

调用失败时如何访问响应标头?我正在捕获 HTTPError 异常,但不知道如何从中获取标头。

我可以使用 exception.read() 获取响应主体,但这只是没有标头的主体。

一些相关代码:

try:
    request = urllib2.Request( url=self.uri+url, headers=self.headers )
    r = urllib2.urlopen(request)
    xml = r.read()
    return parseString( xml )
except urllib2.HTTPError as err:
    logger.debug("EXCEPTION: %s" % err.read() )

I'm using the Harvest API (http://www.getharvest.com/api). When a client goes over it's quota, a 503 response is returned. In that response there should be a header called "Retry-After" that tells me how long to wait before trying again.

How do I access the response headers when the call fails? I'm grabbing the HTTPError exception, but can't figure out how to get the headers out of it.

I can get the response body with exception.read(), but that's just the body without headers.

Some relevant code:

try:
    request = urllib2.Request( url=self.uri+url, headers=self.headers )
    r = urllib2.urlopen(request)
    xml = r.read()
    return parseString( xml )
except urllib2.HTTPError as err:
    logger.debug("EXCEPTION: %s" % err.read() )

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

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

发布评论

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

评论(3

屋顶上的小猫咪 2024-11-14 12:44:24

试试这个:

   logger.debug(err.headers)

它是一本字典,因此使用 err.headers['Retry-After']

Try this:

   logger.debug(err.headers)

It's a dictionary, thus use err.headers['Retry-After']

神经暖 2024-11-14 12:44:24
(Pdb) pp err.__dict__
{'__iter__': <bound method _fileobject.__iter__ of <socket._fileobject object at 0x2b9a8e923950>>,
 'code': 404,
 'fileno': <bound method _fileobject.fileno of <socket._fileobject object at 0x2b9a8e923950>>,
 'fp': <addinfourl at 47942867504160 whose fp = <socket._fileobject object at 0x2b9a8e923950>>,
 'hdrs': <httplib.HTTPMessage instance at 0x2b9a91964a70>,
 'headers': <httplib.HTTPMessage instance at 0x2b9a91964a70>,
 'msg': 'Not Found',
 'next': <bound method _fileobject.next of <socket._fileobject object at 0x2b9a8e923950>>,
 'read': <bound method _fileobject.read of <socket._fileobject object at 0x2b9a8e923950>>,
 'readline': <bound method _fileobject.readline of <socket._fileobject object at 0x2b9a8e923950>>,
 'readlines': <bound method _fileobject.readlines of <socket._fileobject object at 0x2b9a8e923950>>,
 'url': 'http://www.heise.de/fo'}

所有相关的响应信息都可以从捕获的异常中获得。

(Pdb) pp err.__dict__
{'__iter__': <bound method _fileobject.__iter__ of <socket._fileobject object at 0x2b9a8e923950>>,
 'code': 404,
 'fileno': <bound method _fileobject.fileno of <socket._fileobject object at 0x2b9a8e923950>>,
 'fp': <addinfourl at 47942867504160 whose fp = <socket._fileobject object at 0x2b9a8e923950>>,
 'hdrs': <httplib.HTTPMessage instance at 0x2b9a91964a70>,
 'headers': <httplib.HTTPMessage instance at 0x2b9a91964a70>,
 'msg': 'Not Found',
 'next': <bound method _fileobject.next of <socket._fileobject object at 0x2b9a8e923950>>,
 'read': <bound method _fileobject.read of <socket._fileobject object at 0x2b9a8e923950>>,
 'readline': <bound method _fileobject.readline of <socket._fileobject object at 0x2b9a8e923950>>,
 'readlines': <bound method _fileobject.readlines of <socket._fileobject object at 0x2b9a8e923950>>,
 'url': 'http://www.heise.de/fo'}

All related response information are available from the caught exception.

信仰 2024-11-14 12:44:24

err.read() 返回正文,err.info() 返回标头

err.read() returns body, and err.info() returns headers

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