Question

Before I used mod_python for python websites. Unfortunately mod_python is not up to date any more so I looked for another framework and found mod_wsgi.

In mod_python it was possible to have an index method and also other methods. I would like to have more than one page that will be called. Something like this:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

def test(environ, start_response):
    status = '200 OK'
    output = 'Hello test!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

Is that possible with mod_wsgi?

SOLUTION: The Flask framework does what I need

#!/usr/bin/python
from flask import Flask
from flask import request
app = Flask(__name__)
app.debug = True
@app.route("/")
def index():
    return "Hello index"

@app.route("/about")#, methods=['POST', 'GET'])
def about():
    content = "Hello about!!"
    return content

if __name__ == "__main__":
    app.run()
Was it helpful?

Solution

WSGI is a general entry point for webapps, that said, the reason why you find only hello world while searching for mod_wsgi is that you're searching for mod_wsgi and not for framework that implement the standart.

See it as such, wsgi is a bit like an onion. The webserver sends the request to your callable. with 2 parameters: environ and start_response. As far as I can tell start_response, is the function that will send your headers and environ is where all parameters are stored.

You can roll your own framework or use something like pyramid, flask and so on. Each of these frameworks can be bound with wsgi.

You then create a wsgi middleware that will handle the request. You can then parse "PATH_INFO" to macth different callables.

def my_index(environ):
    response_headers = [('Content-type', 'text/plain')]
    return response_headers, environ['PATH_INFO']

def application(env, st):
    response = None
    data = None
    if environ['PATH_INFO'] == '/index':
        response, data = my_index(environ)

    st('200 ok', response)

    return [data]

It's a fairly simple example but then with the environ you can do whatever you want. By itself wsgi does none of the thing you might have been used to with mod_python. It's really just an interface for python for webservers.

edit

As other said in the comments, don't try to roll your own if you don't have any idea of what you are doing. Consider using other frameworks and learn more about it first.

For example, you need to write a proper way to bind a function to a url. As I wrote in my example is pretty bad but should give an idea how it's done in the background. You could handle request using regex to extract ids or use something similar to traversal for pyramid and zope.

If you really insist rolling your own, have a look at the webob tutorial.

http://docs.webob.org/en/latest/do-it-yourself.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top