使用 urllib2 获取错误标头
我需要向 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
e
具有未记录的headers
和hdrs
属性,其中包含服务器发送的 HTTP 标头。顺便说一句,308 不是有效的 HTTP 状态代码 。
e
has undocumentedheaders
andhdrs
properties that contains the HTTP headers sent by the server.By the way, 308 is not a valid HTTP status code.