首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >某些DELETE请求出现Python BadStatusLine错误

某些DELETE请求出现Python BadStatusLine错误
EN

Stack Overflow用户
提问于 2011-11-07 08:45:55
回答 2查看 1.3K关注 0票数 1

我正在尝试使用python-rest-client ( http://code.google.com/p/python-rest-client/wiki/Using_Connection )来执行一些RESTful use服务的测试。因为我刚开始学习,所以我一直将我的测试指向http://www.predic8.com/rest-demo.htm提供的示例服务。

我在创建条目、更新条目或检索条目(POST和GET请求)方面没有任何问题。当我尝试发出DELETE请求时,它失败了。我可以使用Firefox REST客户端执行DELETE请求,它们可以工作。我也可以在其他服务上发出删除请求,但我一直在试图弄清楚为什么它在这种情况下不起作用,这让我自己发疯。我使用的是带有更新的Httplib2的Python3,但我也尝试了Python2.5,这样我就可以在包含的Httplib2版本中使用python-rest-client。我在这两种情况下都看到了同样的问题。

代码很简单,符合文档中的用法:

代码语言:javascript
复制
from restful_lib import Connection        
self.base_url = "http://www.thomas-bayer.com"
self.conn = Connection(self.base_url)
response = self.conn.request_delete('/sqlrest/CUSTOMER/85')

我已经查看了浏览器工具和我的代码产生的HTTP请求,我不明白为什么一个可以工作,另一个不能。这是我收到的跟踪:

代码语言:javascript
复制
Traceback (most recent call last):
 File "/home/fmk/python/rest-client/src/TestExampleService.py", line 68, in test_CRUD
  self.Delete()
 File "/home/fmk/python/rest-client/src/TestExampleService.py", line 55, in Delete
  response = self.conn.request_delete('/sqlrest/CUSTOMER/85')
 File "/home/fmk/python/rest-client/src/restful_lib.py", line 64, in request_delete
  return self.request(resource, "delete", args, headers=headers)
 File "/home/fmk/python/rest-client/src/restful_lib.py", line 138, in request
  resp, content = self.h.request("%s://%s%s" % (self.scheme, self.host,     '/'.join(request_path)), method.upper(), body=body, headers=headers )
 File "/home/fmk/python/rest-client/src/httplib2/__init__.py", line 1175, in request
 (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
 File "/home/fmk/python/rest-client/src/httplib2/__init__.py", line 931, in _request
 (response, content) = self._conn_request(conn, request_uri, method, body, headers)
 File "/home/fmk/python/rest-client/src/httplib2/__init__.py", line 897, in _conn_request
  response = conn.getresponse()
 File "/usr/lib/python3.2/http/client.py", line 1046, in getresponse
  response.begin()
 File "/usr/lib/python3.2/http/client.py", line 346, in begin
  version, status, reason = self._read_status()
 File "/usr/lib/python3.2/http/client.py", line 316, in _read_status
  raise BadStatusLine(line)
 http.client.BadStatusLine: ''

出什么事了?我该怎么做呢?实际上,我很乐意得到调试它的建议。我已经在脚本中更改了域,并将其指向我自己的机器,以便我可以查看请求。我已经在BurpProxy中查看/修改了火狐请求,以使它们与我的脚本请求相匹配。修改后的Burp请求仍然有效,而Python请求仍然无效。

EN

回答 2

Stack Overflow用户

发布于 2011-11-08 03:55:57

显然,问题在于服务器希望有一些用于DELETE请求的消息体。这对于DELETE来说是一种不寻常的期望,但是通过在头文件中指定Content-Length:0,我能够成功地执行删除操作。

代码语言:javascript
复制
from restful_lib import Connection        
self.base_url = "http://www.thomas-bayer.com"
self.conn = Connection(self.base_url)
response = self.conn.request_delete('/sqlrest/CUSTOMER/85', headers={'Content-Length':'0'})

为了证明这个概念,我转到了堆栈跟踪中发生请求的位置:

代码语言:javascript
复制
File "/home/fmk/python/rest-client/src/httplib2/__init__.py", line 897, in _conn_request
response = conn.getresponse()

我在那里打印了headers参数,以确认内容长度不在那里,然后我添加了:

代码语言:javascript
复制
if(method == 'DELETE'):
     headers['Content-Length'] = '0'

在请求之前。

我认为真正的答案是服务是不可靠的,但至少我对httplib2有了更好的了解。我见过其他一些困惑的人在寻求REST和Python的帮助,所以希望我不是唯一一个从中获得一些东西的人。

票数 4
EN

Stack Overflow用户

发布于 2011-11-07 14:39:42

以下脚本正确地从服务器生成404响应:

代码语言:javascript
复制
#!/usr/bin/env python3
import http.client 

h = http.client.HTTPConnection('www.thomas-bayer.com', timeout=10)
h.request('DELETE', '/sqlrest/CUSTOMER/85', headers={'Content-Length': 0})
response = h.getresponse()
print(response.status, response.version)
print(response.info())
print(response.read()[:77])

python -V => 3.2

代码语言:javascript
复制
curl -X DELETE http://www.thomas-bayer.com/sqlrest/CUSTOMER/85
curl: (52) Empty reply from server

Status-Line is not optional服务器必须返回它。或者至少发送411 Length Required响应。

代码语言:javascript
复制
curl -H 'Content-length: 0' -X DELETE \
  http://www.thomas-bayer.com/sqlrest/CUSTOMER/85

正确返回404

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8031636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档