繁体   English   中英

python:在Cherrypy中使用json.load()出现500错误

[英]python: 500 error using json.load() in cherrypy

我的代码在本地运行良好,但是我使用的网络主机出现500错误。 问题似乎来自线路

js = json.load(data)

在搜索方法中。 我在cherrypy配置中缺少什么? 有什么想法吗?

#!/usr/local/bin/python3.2
import cherrypy
import json
import numpy as np
from urllib.request import urlopen

class Root(object):
    @cherrypy.expose
    def index(self):

            a_query = Query()
            text = a_query.search()

            return '''<html>
            Welcome to Spoti.py! %s
            </html>''' %text

class Query():

    def __init__(self):
        self.qstring = '''if i can't'''

    def space_to_plus(self):
        self.qstring = self.qstring.replace(' ', '+')    

    def search(self):
        self.space_to_plus()
        url = 'http://ws.spotify.com/search/1/track.json?q=' + self.qstring
        data = urlopen(url)
        js = json.load(data)
        return self.qstring


cherrypy.config.update({
    'environment': 'production',
    'log.screen': False,
    'server.socket_host': '127.0.0.1',
    'server.socket_port': 15083,
})

cherrypy.config.update({'tools.sessions.on': True})

cherrypy.quickstart(Root())

给出字节后,似乎json.loads()期望使用unicode对象。 尝试这个:

data = urlopen(url).read()
js = json.loads(data.decode('utf-8'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM