python: 500 error using json.load() in cherrypy
Question: My code runs fine locally but I get a 500 error with the webhost I'm using.
The problem seems to come from the line

    
    
    js = json.load(data)
    

in the search method. Is there something in the cherrypy config that I'm
missing? Any thoughts?

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


Answer: It seems json.loads() is expecting a unicode object when bytes are given. Try
this:

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

