I am trying to write a WSGI app using Python bottle. I installed bottle and now I run it together with Apache's mod_wsgi module, as it is described here : http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
What I would like to do is to return a JSON file depending on the URL (request). I managed to do that but I think it is not the right way as it is full of workarounds. What I mean is that 1 I cannot return a JSON variable, because Apache complains about
RuntimeError: response has not been started
2 mod_wsgi requires my callable object to named like "application" and takes 2 arguments, meaning that I cannot use the "@route" attributes, as it is described here : http://webpython.codepoint.net/wsgi_application_interface
So, for 1 I used the json.dumps method and for 2 I take the route as an environment variable. Could you please enlighten me on how to use the "@route" attribute and the best practices of Python bottle in this case ? How I deal with these 2 issues appears below :
#!/usr/bin/python
import sys, os, time
import json
import MySQLdb
import bottle
import cgi
os.chdir( os.path.dirname( __file__ ) )
bottle.debug( True )
application = bottle.default_app( )
@bottle.route( '/wsgi/<parameter>' )
def application( environ, start_response ) :
# URL = bottle.request.method
URL = environ["PATH_INFO"]
status = '200 OK'
response_headers = [('Content-type', 'application/json')]
start_response( status, response_headers )
demo = { 'status':'online', 'servertime':time.time(), 'url':URL }
demo = json.dumps( demo )
return demo