簡體   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