Note that there are some explanatory texts on larger screens.

plurals
  1. POPython bottle : return JSON and @route attributes related
    primarykey
    data
    text
    <p>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 : <a href="http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide" rel="nofollow">http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide</a></p> <p>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 <a href="http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide" rel="nofollow">1</a> I cannot return a JSON variable, because Apache complains about</p> <pre><code>RuntimeError: response has not been started </code></pre> <p><a href="http://webpython.codepoint.net/wsgi_application_interface" rel="nofollow">2</a> 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 : <a href="http://webpython.codepoint.net/wsgi_application_interface" rel="nofollow">http://webpython.codepoint.net/wsgi_application_interface</a></p> <p>So, for <a href="http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide" rel="nofollow">1</a> I used the json.dumps method and for <a href="http://webpython.codepoint.net/wsgi_application_interface" rel="nofollow">2</a> 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 :</p> <pre><code>#!/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/&lt;parameter&gt;' ) 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload