0
votes

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
1
You are not writing a bottle application here. Your code looks like a bare WSGI application with a bottle decorator in front of it (which makes no sense). You should have a look at the bottle documentation.defnull
: thank you defnull for your response, do you know what i am doing wrong regarding the routes ?user690182

1 Answers

0
votes

As the comments state , you should write a bottle application like this ( adapted from your code ):

#!/usr/bin/python

import  sys, os, time
import  bottle

@bottle.route( '/wsgi/<parameter>' )
def application(parameter):
    return {'status':'online', 'servertime': time.time(), 'url': bottle.request.path}

if __name__ == '__main__':
    bottle.run(host="localhost", port=8080, debug=True)

Test with curl :

$curl -i http://127.0.0.1:8080/wsgi/test
HTTP/1.0 200 OK
Date: Tue, 02 Apr 2013 09:25:18 GMT
Server: WSGIServer/0.1 Python/2.7.3
Content-Length: 73
Content-Type: application/json

{"status": "online", "url": "/wsgi/test", "servertime": 1364894718.82041}