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

标签 python json load cherrypy

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

js = json.load(data)

在搜索方法中。樱桃配置中是否有我遗漏的东西?有什么想法吗?

#!/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'))

关于python:在cherrypy中使用json.load()时出现500错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18800690/

相关文章:

python - 判断线程是否已经启动

sql-server - 解析 SQL Server 集成服务包中的 JSON 数据?

json - FabricJS - 从 JSON 加载 Canvas 时出错

amazon-web-services - 向AWS ELB后面的所有计算机发送请求

load - Visual Studio 2015 CTP 6 Windows 10 项目设计器加载失败

python - 如何将 wiris 添加到 django-ckeditor

Python Beautifulsoup,在标签中获取 href 标签

python - 使用 python etree 从 xml 中删除模式

jquery - JSON 字符串中的冒号

java - 解析 CloudWatch 日志流的 AWS Lambda