【问题标题】:Python Cherrypy: 500 ValueError: Page handlers MUST return bytesPython Cherrypy:500 ValueError:页面处理程序必须返回字节
【发布时间】:2013-11-26 10:57:37
【问题描述】:

我从提交模块生成的cherrypy 脚本中收到以下错误。

ValueError:页面处理程序必须返回字节。如果您希望返回 unicode,请使用 tools.encode

我在配置中打开了 tool.encode,但我仍然收到此错误。我允许用户通过 jQuery 表单插件上传内容。关于我为什么会收到此错误的任何想法?

这是我的樱桃文件:

class Root(object):    

@cherrypy.expose
def index(self)
    return open('/home/joestox/webapps/freelinreg_static/index.html')

@cherrypy.expose
def submit(self, myfile):

    cherrypy.session['myfile'] = myfile
    data_name = myfile.filename

    #Send back to JQuery with Ajax
    #Put in JSON form
    data_name= json.dumps(dict(title = data_name))
    cherrypy.response.headers['Content-Type'] = 'application/json'

    return data_name



cherrypy.config.update({
    'tools.staticdir.debug': True,
    'log.screen': True,
    'server.socket_host': '127.0.0.1',
    'server.socket_port': *****,
    'tools.sessions.on': True,
    'tools.encode.on': True,
    'tools.encode.encoding': 'utf-8',
})

config = {
}

cherrypy.tree.mount(Root(), '/', config=config)
cherrypy.engine.start()

HTML:

<!DOCTYPE html>
    <html>
        <head> 
            <script type='text/javascript' src='freelinreg_static/google.js'></script>
            <script type='text/javascript' src='freelinreg_static/frontend.js'></script>
            <script type='text/javascript' src='freelinreg_static/malsup.js'></script>
        </head>
        <body>

        <form id="dataform" action="submit" method="post" enctype="multipart/form-data">
            <input type="file" name="myfile" id="myFile"/>
            <input type="submit" id="data_submit" value="Continue"/>
        </form>                          

        </body>
    </html>

jQuery (frontend.js):

$(document).ready(function () {
    (function () {
        $('#dataform').ajaxForm({
            url: "submit",
            success: function (data) {
                var $a_var = data['title'];
                $('body').append($a_var);
            }
        });
        return false;
    })();
});

【问题讨论】:

    标签: python unicode cherrypy


    【解决方案1】:

    我的问题是从 python2 切换到 python3 后开始的。

    通过设置解决

        'tools.encode.text_only': False
    

    在应用全局配置中。

    希望对你有帮助

    【讨论】:

    • 查看其他答案后,这对我有用。谢谢。
    【解决方案2】:

    大家好,正在寻找答案。 我遇到了同样的问题,但就我而言,这个小小的添加解决了所有问题。

    return <some-json>.encode('utf8')
    

    【讨论】:

    • 使用 json_out 工具,只返回一个字典
    • @webKnjaZ 很好地解决了我的问题,我遇到了同样的错误,这两个词解决了它,所以我很高兴
    • 是的,这不是最好的架构方式。
    【解决方案3】:

    您需要重新安排全局配置更新以在应用程序挂载后发生:

    config = {
    }
    
    cherrypy.tree.mount(Root(), '/', config=config)
    
    cherrypy.config.update({
        'tools.staticdir.debug': True,
        'log.screen': True,
        'server.socket_host': '127.0.0.1',
        'server.socket_port': *****,
        'tools.sessions.on': True,
        'tools.encode.on': True,
        'tools.encode.encoding': 'utf-8'
    })
    
    cherrypy.engine.start()
    

    因为您在 config update 命令之后调用了 config = {},所以您覆盖了 Root 应用程序的更新设置。

    另外,将您的提交功能更改为:

    @cherrypy.expose
    @cherrypy.tools.json_out
    def submit(self, myfile):
        cherrypy.session['myfile'] = myfile
    
        # Return dict, which will be autoconverted to JSON
        # by the json_out tool (see decorator above)
        return {'title': myfile.filename}
    

    【讨论】:

    • 感谢您的提示!很有意义!不幸的是,我仍然得到相同的 500 ValueError。
    • 成功了!无论如何要将数据作为 JSON dict 发送回 jQuery 吗?现在 jQuery 似乎没有意识到它是一个 JSON 字典。
    • 是的,你应该使用... JSONobj = JSON.parse(data);警报(JSONobj.title);
    • 使用 json_out 工具从 dict 返回 json
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2018-06-22
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    相关资源
    最近更新 更多