cherrypy.request.body.read() 错误
我在使用 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)
导致错误的代码是:
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()
i可以很好地浏览请求的主体。我尝试用谷歌搜索解决方案,但没有找到。考虑到我是一个完全的Python新手,我一定做错了什么,但是什么?
预先感谢您的宝贵帮助。
I'm having some problems accessing http requests' bodies with the CherryPy framework.
I'm using CherryPy 3.2.0 on a x86_64 Arch Linux machine w/ Python3 and Aptana Web Studio IDE.
When I try to access a request's body via the usual cherrypy.request.body.read(), i get the error
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)
The code causing the error is:
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())
However, using
cherrypy.request.body.readline() or cherrypy.request.body.readlines(n)
instead of
cherrypy.request.body.read()
i can skim through the request's body just fine. I tried googling for a solution but found none. Considering i'm a total python newbie, there must be something i am doing wrong, but what?
Thanks in advance for your precious help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当请求正文已处理时,
body.read()
方法才能正常工作,只有当request.process_request_body
为 True(默认情况下)并且请求method 位于 request.method_with_bodies 中,默认情况下仅是 PUT 和 POST,而不是 GET(您在使用浏览器请求页面时可能会使用 GET)。The
body.read()
method works properly only if the request body was processed, which happens only whenrequest.process_request_body
is True (it is by default) and when the request method is inrequest.method_with_bodies
which is only PUT and POST by default, but not GET (which you probably used when requesting the page with a browser).