2011-11-02 113 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