我正在尝试使用 python-rest-client ( http://code.google.com/p/python-rest-client/wiki/Using_Connection ) 来执行一些 RESTful 网络服务的测试。由于我刚刚学习,我一直将测试指向http://www.predic8.com/rest-demo.htm提供的示例服务。
我在创建条目、更新条目或检索条目(POST 和 GET 请求)方面没有问题。当我尝试发出 DELETE 请求时,它失败了。我可以使用 Firefox REST Client 来执行 DELETE 请求并且它们可以工作。我也可以在其他服务上发出 DELETE 请求,但我一直在让自己发疯,试图弄清楚为什么它在这种情况下不起作用。我正在使用带有更新的 Httplib2 的 Python 3,但我也尝试了 Python 2.5,以便可以将 python-rest-client 与包含的 Httplib2 版本一起使用。在这两种情况下,我都看到了同样的问题。
代码很简单,与记录的使用相匹配:
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 请求,但我不明白为什么一个有效而另一个无效。这是我收到的跟踪:
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 中的 Firefox 请求,以使它们与我的脚本请求相匹配。修改后的 Burp 请求仍然有效,而 Python 请求仍然无效。