2011-11-02 112 views
3

我在使用CherryPy框架访问http请求的主体时遇到了一些问题。 我在具有Python3和Aptana Web Studio IDE的x86_64 Arch Linux机器上使用CherryPy 3.2.0。cherrypy.request.body.read()error

当我尝试通过平时的cherrypy.request.body.read(),我得到的错误访问请求的身体

File "/usr/lib/python3.2/site-packages/cherrypy/_cpreqbody.py", line 450, in read 
return self.fp.read(size, fp_out) 
TypeError: read() takes at most 2 positional arguments (3 given) 

导致错误的代码是:

import cherrypy 

class Test: 
    def index(self): 
     print(cherrypy.request.body.read()) 
     #print(cherrypy.request.body.readline()) <- this works! 
    return 'HelloWorld' 
index.exposed = True 

if __name__ == '__main__': 
    cherrypy.quickstart(Test()) 

然而,使用

cherrypy.request.body.readline() or cherrypy.request.body.readlines(n) 

,而不是

cherrypy.request.body.read() 

我可以浏览请求的身体就好了。我试着用Google搜索解决方案,但没有发现。考虑到我是一个总的python新手,必须有一些我做错了,但什么?

在此先感谢您的宝贵帮助。

+0

'print(type(cherrypy.request.body.fp))'给了你什么? (就在'print'行之前。) – jro

回答

8

body.read()法正常工作只有在请求体被处理,其中仅发生request.process_request_body为True(这是默认设置),当请求方法是request.method_with_bodies其默认情况下只PUT和POST,而不是GET (您可能会在使用浏览器请求页面时使用该功能)。

+0

感谢您的解释!抛出的异常必须是我见过的最不具描述性的... – johndodo