使用 urllib2 获取错误标头

发布于 2024-11-16 04:09:39 字数 380 浏览 4 评论 0原文

我需要向 Web 服务发送 PUT 请求,并从错误标头中获取一些数据,这是请求的预期结果。代码如下:

Request = urllib2.Request(destination_url, headers=headers)
Request.get_method = lambda: 'PUT'

try:
   Response = urllib2.urlopen(Request)
except urllib2.HTTPError, e:
   print 'Error code: ', e.code
   print e.read()

我收到错误 308,但响应为空,并且我没有从 HTTPError 中获取任何数据。有没有办法在出现 HTTP 错误时获取 HTTP 标头?

I need to send a PUT request to a web service and get some data out of error headers that is the expected result of the request. The code goes like this:

Request = urllib2.Request(destination_url, headers=headers)
Request.get_method = lambda: 'PUT'

try:
   Response = urllib2.urlopen(Request)
except urllib2.HTTPError, e:
   print 'Error code: ', e.code
   print e.read()

I get Error 308 but response is empty and I'm not getting any data out of HTTPError. Is there a way to get HTTP headers while getting an HTTP error?

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

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

发布评论

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

评论(1

遇到 2024-11-23 04:09:39

e 具有未记录的 headershdrs 属性,其中包含服务器发送的 HTTP 标头。

顺便说一句,308 不是有效的 HTTP 状态代码

e has undocumented headers and hdrs properties that contains the HTTP headers sent by the server.

By the way, 308 is not a valid HTTP status code.

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