Jac*_*caz 3 python cherrypy request
我在使用CherryPy框架访问http请求的主体时遇到了一些问题.我在带有Python3和Aptana Web Studio IDE的x86_64 Arch Linux机器上使用CherryPy 3.2.0.
当我尝试通过通常的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)
Run Code Online (Sandbox Code Playgroud)
导致错误的代码是:
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())
Run Code Online (Sandbox Code Playgroud)
但是,使用
cherrypy.request.body.readline() or cherrypy.request.body.readlines(n)
Run Code Online (Sandbox Code Playgroud)
代替
cherrypy.request.body.read()
Run Code Online (Sandbox Code Playgroud)
我可以很好地浏览请求的正文.我试着谷歌搜索解决方案,但没有找到.考虑到我是一个完全蟒蛇新手,一定有我做错了什么,但是什么?
提前感谢您的宝贵帮助.
该body.read()方法只有在处理请求体时才能正常工作,只有在request.process_request_body为True时(默认情况下)才发生,并且当请求方法request.method_with_bodies默认为PUT和POST时,但不是GET(在请求时可能使用)带浏览器的页面).