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