Guru Guru - 1 year ago 54
Python Question

Unable to create REST service in python

I want to create a REST service, So I tried and here's my snippet

from bottle import route, run

def plot_graph():
#compute graph_list (python object of type list)
return graph_list

if __name__ == "__main__":
run(host='', port=8881, server='cherrypy', debug=True)

Now when I enter this in browser http://localhost:8881/plot_graph it gives error

Error: 500 Internal Server Error

Sorry, the requested URL 'http://localhost:8881/plot_graph' caused an error:

Unsupported response type: <type 'int'>

and my python console says that it is listening but gives this warning

Bottle v0.12.9 server starting up (using CherryPyServer())...
Listening on
Hit Ctrl-C to quit.

/Users/guru/python_projects/implement_LDA/lda/lib/python2.7/site-packages/ ImportWarning: Not importing directory '/Users/guru/python_projects/implement_LDA/lda/cherrypy': missing
from cherrypy import wsgiserver

Any ways to resolve this?

Answer Source

graph_list needs to contain strings, however, it looks like your list contains integers. You could convert these integers to strings with this:

return (str(i) for i in graph_list)

But note that the elements of the list are joined together which might not be what you want.

For the warning problem, it would appear that you have a directory named cherrypy in the same directory as your main python script. Rename/remove that directory and bottle will import CherryPy from your site-packages directory. Or you could simply remove server='cherrypy' from the call to run() to use the default wsgiref server.