cherrypy.request.body.read() 错误

发布于 2024-12-13 07:44:51 字数 972 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-12-20 07:44:51

仅当请求正文已处理时,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 when request.process_request_body is True (it is by default) and when the request method is in request.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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文