2013-03-20 35 views
1

我使用gae和Python 2.7,升级到gae 1.7.6后,我的单元测试被破坏。我正在使用nose和nosega进行单元测试。有谁知道发生了什么事?任何想法将非常感激。我觉得这与webob 1.2.3被升级到GA有关。升级到appengine后断开连接错误1.7.6

self.app.post(task['url'], params, headers) 
    File "/Library/Python/2.7/site-packages/WebTest-1.4.0-py2.7.egg/webtest/app.py", line 835, in post 
    content_type=content_type) 
    File "/Library/Python/2.7/site-packages/WebTest-1.4.0-py2.7.egg/webtest/app.py", line 807, in _gen_request 
    expect_errors=expect_errors) 
    File "/Library/Python/2.7/site-packages/WebTest-1.4.0-py2.7.egg/webtest/app.py", line 1118, in do_request 
    self._check_status(status, res) 
    File "/Library/Python/2.7/site-packages/WebTest-1.4.0-py2.7.egg/webtest/app.py", line 1154, in _check_status 
    res) 
AppError: Bad response: 500 Internal Server Error (not 200 OK or 3xx redirect for http://localhost/task/request_log) 
<pre>Traceback (most recent call last): 
    File &quot;/Users/***/Downloads/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py&quot;, line 1089, in __call__ 
    method(*args, **kwargs) 
    File &quot;/Users/***/projects/game_server/game_service/events.py&quot;, line 184, in post 
    inputs = pickle.loads(self.request.body) 
    File &quot;/Users/***/Downloads/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob-1.2.3/webob/request.py&quot;, line 677, in _body__get 
    self.make_body_seekable() # we need this to have content_length 
    File &quot;/Users/***/Downloads/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob-1.2.3/webob/request.py&quot;, line 922, in make_body_seekable 
    self.copy_body() 
    File &quot;/Users/***/Downloads/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob-1.2.3/webob/request.py&quot;, line 945, in copy_body 
    self.body = self.body_file.read(self.content_length) 
    File &quot;/Users/***/Downloads/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob-1.2.3/webob/request.py&quot;, line 1528, in readinto 
    + &quot;(%d more bytes were expected)&quot; % self.remaining 
DisconnectionError: The client disconnected while sending the POST/PUT body (151 more bytes were expected) 
</pre> 
    self.app.post(task['url'], params, headers) 
    File "/Library/Python/2.7/site-packages/WebTest-1.4.0-py2.7.egg/webtest/app.py", line 835, in post 
    content_type=content_type) 
    File "/Library/Python/2.7/site-packages/WebTest-1.4.0-py2.7.egg/webtest/app.py", line 807, in _gen_request 
    expect_errors=expect_errors) 
    File "/Library/Python/2.7/site-packages/WebTest-1.4.0-py2.7.egg/webtest/app.py", line 1118, in do_request 
    self._check_status(status, res) 
    File "/Library/Python/2.7/site-packages/WebTest-1.4.0-py2.7.egg/webtest/app.py", line 1154, in _check_status 
    res) 
AppError: Bad response: 500 Internal Server Error (not 200 OK or 3xx redirect for http://localhost/task/request_log) 
<pre>Traceback (most recent call last): 
    File &quot;/Users/***/Downloads/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py&quot;, line 1089, in __call__ 
    method(*args, **kwargs) 
    File &quot;/Users/***/projects/game_server/game_service/events.py&quot;, line 184, in post 
    inputs = pickle.loads(self.request.body) 
    File &quot;/Users/***/Downloads/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob-1.2.3/webob/request.py&quot;, line 677, in _body__get 
    self.make_body_seekable() # we need this to have content_length 
    File &quot;/Users/***/Downloads/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob-1.2.3/webob/request.py&quot;, line 922, in make_body_seekable 
    self.copy_body() 
    File &quot;/Users/***/Downloads/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob-1.2.3/webob/request.py&quot;, line 945, in copy_body 
    self.body = self.body_file.read(self.content_length) 
    File &quot;/Users/***/Downloads/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob-1.2.3/webob/request.py&quot;, line 1528, in readinto 
    + &quot;(%d more bytes were expected)&quot; % self.remaining 
DisconnectionError: The client disconnected while sending the POST/PUT body (151 more bytes were expected) 
</pre> 

回答

1

简短版本:headers可能包含错误的内容长度;修复它像这样(假设paramsstr):

fixed_header = ('Content-Length', str(len(params))) 
for i, header in enumerate(headers): 
    if header[0] == 'Content-Length': 
     headers[i] = fixed_header 
     fixed_header = None 
     break 
if fixed_header: # in case you're completely missing a Content-Length header 
    headers.append(fixed_header) 

GAE 1.7.6改变了默认的WebOb从V0.9至V1.2.3。用于检索请求参数的代码(例如,通过webob.Request.get())在这些版本之间改变。

在1.2.3,如果你看一下BaseRequest.params它创建了一个NestedMultiDict使用BaseRequest.POST这反过来又尝试读取使用cgi.FieldStorage()它检查Content-Length头的请求的主体。如果内容长度不正确,您会看到您遇到的DisconnectionError

在0.9,Request.params使用Request.str_params构建,其使用Request.str_POST(不同于1.2.3)构建其NestedMultiDict。这反过来使用已有的Request.body_ file,其已经与StringIO()一起使用,没有检查内容长度报头

因此,您可以在1.7.5中使用不正确的Content-Length标头,但不在1.7.6中。

任务队列数据是Base64编码的。我怀疑你在回溯的第一行传入的params已经从其Base64格式解码,但是您没有更新headers中的Content-Length标头以反映这一点。我怀疑你从任务队列上的GetTasks()获得了任务的头部,该任务队列的Content-Length设置为原始Base64编码数据的长度。

0

您要发送的请求可能缺少适当的Content-Length标头。

没有它,服务器无法确定客户端是否已上传所有数据,或者他是否在传输过程中断开连接。

+0

有趣的,不知道为什么发生这种情况,因为请求是由WebTest准备的,并且只有在升级到appengine 1.7.6时才会中断。我已经浏览了webtest代码,并且代码设置了内容长度 – marcadian 2013-03-26 18:33:14

+1

我在1.7.6中遇到了很多问题。我回到了old_dev_appserver。 – Mohamed 2013-03-27 23:52:52

+1

是的,我现在也恢复到1.7.5 – marcadian 2013-03-28 19:50:33