File: wsgi.py

package info (click to toggle)
libapache2-mod-python 3.5.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,864 kB
  • sloc: python: 7,471; ansic: 7,025; makefile: 296; lex: 246; sh: 212
file content (80 lines) | stat: -rw-r--r-- 2,645 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 #
 # Copyright (C) 2000, 2001, 2013 Gregory Trubetskoy
 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Apache Software Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License"); you
 # may not use this file except in compliance with the License.  You
 # may obtain a copy of the License at
 #
 #      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 # implied.  See the License for the specific language governing
 # permissions and limitations under the License.
 #
 # Originally developed by Gregory Trubetskoy.
 #

import sys
from mod_python import apache

def handler(req):

    options = req.get_options()

    ## Find the application callable

    app = None
    app_str = options['mod_python.wsgi.application']
    if app_str:
        if '::' in app_str:
            mod_str, callable_str = app_str.split('::', 1)
        else:
            mod_str, callable_str = app_str, 'application'
        config = req.get_config()
        autoreload, log = True, False
        if "PythonAutoReload" in config:
            autoreload = config["PythonAutoReload"] == "1"
        if "PythonDebug" in config:
            log = config["PythonDebug"] == "1"
        module = apache.import_module(mod_str, autoreload=autoreload, log=log)

        try:
            app = module.__dict__[callable_str]
        except KeyError: pass

    if not app:
        req.log_error(
            'WSGI handler: mod_python.wsgi.application (%s) not found, declining.'
            % repr(app_str), apache.APLOG_WARNING)
        return apache.DECLINED

    ## Build env

    env = req.build_wsgi_env()
    if env is None:
        # None means base_uri mismatch. The problem can be either
        # base_uri or Location, but because we couldn't be here if it
        # was Location, then it must be mod_python.wsgi.base_uri.
        base_uri = options.get('mod_python.wsgi.base_uri')
        req.log_error(
            "WSGI handler: req.uri (%s) does not start with mod_python.wsgi.base_uri (%s), declining."
            % (repr(req.uri), repr(base_uri)), apache.APLOG_WARNING)
        return apache.DECLINED

    ## Run the app

    response = None
    try:
        response = app(env, req.wsgi_start_response)
        [req.write(token) for token in response]
    finally:
        # call close() if there is one
        if type(response) not in (list, tuple):
            getattr(response, 'close', lambda: None)()

    return apache.OK