OpenSecurity/install/web.py-0.37/build/lib/web/wsgi.py
changeset 3 65432e6c6042
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/OpenSecurity/install/web.py-0.37/build/lib/web/wsgi.py	Mon Dec 02 14:02:05 2013 +0100
     1.3 @@ -0,0 +1,70 @@
     1.4 +"""
     1.5 +WSGI Utilities
     1.6 +(from web.py)
     1.7 +"""
     1.8 +
     1.9 +import os, sys
    1.10 +
    1.11 +import http
    1.12 +import webapi as web
    1.13 +from utils import listget
    1.14 +from net import validaddr, validip
    1.15 +import httpserver
    1.16 +    
    1.17 +def runfcgi(func, addr=('localhost', 8000)):
    1.18 +    """Runs a WSGI function as a FastCGI server."""
    1.19 +    import flup.server.fcgi as flups
    1.20 +    return flups.WSGIServer(func, multiplexed=True, bindAddress=addr, debug=False).run()
    1.21 +
    1.22 +def runscgi(func, addr=('localhost', 4000)):
    1.23 +    """Runs a WSGI function as an SCGI server."""
    1.24 +    import flup.server.scgi as flups
    1.25 +    return flups.WSGIServer(func, bindAddress=addr, debug=False).run()
    1.26 +
    1.27 +def runwsgi(func):
    1.28 +    """
    1.29 +    Runs a WSGI-compatible `func` using FCGI, SCGI, or a simple web server,
    1.30 +    as appropriate based on context and `sys.argv`.
    1.31 +    """
    1.32 +    
    1.33 +    if os.environ.has_key('SERVER_SOFTWARE'): # cgi
    1.34 +        os.environ['FCGI_FORCE_CGI'] = 'Y'
    1.35 +
    1.36 +    if (os.environ.has_key('PHP_FCGI_CHILDREN') #lighttpd fastcgi
    1.37 +      or os.environ.has_key('SERVER_SOFTWARE')):
    1.38 +        return runfcgi(func, None)
    1.39 +    
    1.40 +    if 'fcgi' in sys.argv or 'fastcgi' in sys.argv:
    1.41 +        args = sys.argv[1:]
    1.42 +        if 'fastcgi' in args: args.remove('fastcgi')
    1.43 +        elif 'fcgi' in args: args.remove('fcgi')
    1.44 +        if args:
    1.45 +            return runfcgi(func, validaddr(args[0]))
    1.46 +        else:
    1.47 +            return runfcgi(func, None)
    1.48 +    
    1.49 +    if 'scgi' in sys.argv:
    1.50 +        args = sys.argv[1:]
    1.51 +        args.remove('scgi')
    1.52 +        if args:
    1.53 +            return runscgi(func, validaddr(args[0]))
    1.54 +        else:
    1.55 +            return runscgi(func)
    1.56 +    
    1.57 +    return httpserver.runsimple(func, validip(listget(sys.argv, 1, '')))
    1.58 +    
    1.59 +def _is_dev_mode():
    1.60 +    # Some embedded python interpreters won't have sys.arv
    1.61 +    # For details, see https://github.com/webpy/webpy/issues/87
    1.62 +    argv = getattr(sys, "argv", [])
    1.63 +
    1.64 +    # quick hack to check if the program is running in dev mode.
    1.65 +    if os.environ.has_key('SERVER_SOFTWARE') \
    1.66 +        or os.environ.has_key('PHP_FCGI_CHILDREN') \
    1.67 +        or 'fcgi' in argv or 'fastcgi' in argv \
    1.68 +        or 'mod_wsgi' in argv:
    1.69 +            return False
    1.70 +    return True
    1.71 +
    1.72 +# When running the builtin-server, enable debug mode if not already set.
    1.73 +web.config.setdefault('debug', _is_dev_mode())