Guru Guru - 4 months ago 16
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

@route('/plot_graph',method='GET')
def plot_graph():
#compute graph_list (python object of type list)
#done
return graph_list

if __name__ == "__main__":
run(host='0.0.0.0', 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 http://0.0.0.0:8881/
Hit Ctrl-C to quit.

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


Any ways to resolve this?

Answer

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.