Search code examples
phpjsonclasspicklecherrypy

CherryPy & php: can't load class of data from pickle, but works from ssh on server


I am using CherryPy to generate and parse data for a php webpage, but getting an error that I can't replicate locally or via SSH (logging into the server and running the python script from the prompt works okay).

The current sticky error message is:

File "modules/flex.py", line 335, in convert
    ref_data = cPickle.load(f2)
AttributeError: 'module' object has no attribute 'Data'

Another pickle loads fine in a previous line, and both pickles represent class objects with a variety of dictionaries and lists of lists. But one of these works, and the other doesn't.

In general with CherryPy, what's the best way to isolate error messages? I can run the /cp/ function from it's URL, and I can look at the webserver error log, but even these two don't always give an error message (or the correct error).

On the other side, in php, I am using this code to retrieve a json object representing a python list of lists:

$obj = file_get_contents($senddata);
$sue = json_decode($obj);

Is that an acceptable method? What's the best way to pass and generate php arrays from python data structures?


Solution

  • I think that you have different issues:

    1. Your code is not working because the PYTHONPATH (sys.path) are not the same in your python interpreter, when you are connected trough ssh and in the python that is running to serve the webapp. I say that because you are tying to deserialize the data from a module, which it does exists (otherwise will be ImportError) but do not have the class declaration of Data, probably a file with the same name as that module is in the same directory and trying to find the class Data in that file. When you serialize you don't save the class declaration, just how to reconstruct that object.

    2. What do you mean with "the correct error"?, when the environment is "production", CherryPy by default log the traceback of the last error, you can of course change the default behavior with custom error handling.

    3. You should stick with JSON for communication betweeen languages, is pretty standard and efficient to be parsed in both sides.

    I hope that helps.