OpenSecurity/install/web.py-0.37/web/wsgi.py
author om
Mon, 02 Dec 2013 14:02:05 +0100
changeset 3 65432e6c6042
permissions -rwxr-xr-x
initial deployment and project layout commit
     1 """
     2 WSGI Utilities
     3 (from web.py)
     4 """
     5 
     6 import os, sys
     7 
     8 import http
     9 import webapi as web
    10 from utils import listget
    11 from net import validaddr, validip
    12 import httpserver
    13     
    14 def runfcgi(func, addr=('localhost', 8000)):
    15     """Runs a WSGI function as a FastCGI server."""
    16     import flup.server.fcgi as flups
    17     return flups.WSGIServer(func, multiplexed=True, bindAddress=addr, debug=False).run()
    18 
    19 def runscgi(func, addr=('localhost', 4000)):
    20     """Runs a WSGI function as an SCGI server."""
    21     import flup.server.scgi as flups
    22     return flups.WSGIServer(func, bindAddress=addr, debug=False).run()
    23 
    24 def runwsgi(func):
    25     """
    26     Runs a WSGI-compatible `func` using FCGI, SCGI, or a simple web server,
    27     as appropriate based on context and `sys.argv`.
    28     """
    29     
    30     if os.environ.has_key('SERVER_SOFTWARE'): # cgi
    31         os.environ['FCGI_FORCE_CGI'] = 'Y'
    32 
    33     if (os.environ.has_key('PHP_FCGI_CHILDREN') #lighttpd fastcgi
    34       or os.environ.has_key('SERVER_SOFTWARE')):
    35         return runfcgi(func, None)
    36     
    37     if 'fcgi' in sys.argv or 'fastcgi' in sys.argv:
    38         args = sys.argv[1:]
    39         if 'fastcgi' in args: args.remove('fastcgi')
    40         elif 'fcgi' in args: args.remove('fcgi')
    41         if args:
    42             return runfcgi(func, validaddr(args[0]))
    43         else:
    44             return runfcgi(func, None)
    45     
    46     if 'scgi' in sys.argv:
    47         args = sys.argv[1:]
    48         args.remove('scgi')
    49         if args:
    50             return runscgi(func, validaddr(args[0]))
    51         else:
    52             return runscgi(func)
    53     
    54     return httpserver.runsimple(func, validip(listget(sys.argv, 1, '')))
    55     
    56 def _is_dev_mode():
    57     # Some embedded python interpreters won't have sys.arv
    58     # For details, see https://github.com/webpy/webpy/issues/87
    59     argv = getattr(sys, "argv", [])
    60 
    61     # quick hack to check if the program is running in dev mode.
    62     if os.environ.has_key('SERVER_SOFTWARE') \
    63         or os.environ.has_key('PHP_FCGI_CHILDREN') \
    64         or 'fcgi' in argv or 'fastcgi' in argv \
    65         or 'mod_wsgi' in argv:
    66             return False
    67     return True
    68 
    69 # When running the builtin-server, enable debug mode if not already set.
    70 web.config.setdefault('debug', _is_dev_mode())