gpt4 book ai didi

某些 DELETE 请求的 Python BadStatusLine 错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:17:04 26 4
gpt4 key购买 nike

我正在尝试使用 python-rest-client ( http://code.google.com/p/python-rest-client/wiki/Using_Connection ) 来执行一些 RESTful web 服务的测试。因为我只是在学习,所以我一直将我的测试指向 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 请求仍然无效。

最佳答案

显然,问题是服务器希望有一些用于 DELETE 请求的消息正文。这是对 DELETE 的不寻常期望,但通过在 header 中指定 Content-Length:0,我能够成功执行 DELETE。

一路上的某个地方(在 python-rest-client 或 httplib2 中),如果我尝试这样做,Content-Length header 会被清除:

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'})

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

File "/home/fmk/python/rest-client/src/httplib2/__init__.py", line 897, in _conn_request
response = conn.getresponse()

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

if(method == 'DELETE'):
headers['Content-Length'] = '0'

在请求之前。

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

关于某些 DELETE 请求的 Python BadStatusLine 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8031636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com