mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Make WSGI app names are now evaled in the module namespace.
This allows people to pass info from the command line to a WSGI
application. See examples/alt_spec.py for code that uses this
method. Example invocation:
$ gunicorn 'alt_spec:load("my arg here")'
Notice the single quotes to avoid shell escape semantics.
Closes #56
Closes #40
This commit is contained in:
parent
80f4d17122
commit
3ad7c1b395
25
examples/alt_spec.py
Normal file
25
examples/alt_spec.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# -*- coding: utf-8 -
|
||||||
|
#
|
||||||
|
# An example of how to pass information from the command line to
|
||||||
|
# a WSGI app. Only applies to the native WSGI workers used by
|
||||||
|
# Gunicorn sync (default) workers.
|
||||||
|
#
|
||||||
|
# $ gunicorn 'alt_spec:load(arg)'
|
||||||
|
#
|
||||||
|
# Single quoting is generally necessary for shell escape semantics.
|
||||||
|
#
|
||||||
|
# This file is part of gunicorn released under the MIT license.
|
||||||
|
# See the NOTICE for more information.
|
||||||
|
|
||||||
|
def load(arg):
|
||||||
|
def app(environ, start_response):
|
||||||
|
data = 'Hello, %s!\n' % arg
|
||||||
|
status = '200 OK'
|
||||||
|
response_headers = [
|
||||||
|
('Content-type','text/plain'),
|
||||||
|
('Content-Length', str(len(data)))
|
||||||
|
]
|
||||||
|
start_response(status, response_headers)
|
||||||
|
return iter([data])
|
||||||
|
return app
|
||||||
|
|
||||||
@ -10,6 +10,7 @@ import os
|
|||||||
import pkg_resources
|
import pkg_resources
|
||||||
import resource
|
import resource
|
||||||
import socket
|
import socket
|
||||||
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@ -174,13 +175,9 @@ def import_app(module):
|
|||||||
module, obj = module, "application"
|
module, obj = module, "application"
|
||||||
else:
|
else:
|
||||||
module, obj = parts[0], parts[1]
|
module, obj = parts[0], parts[1]
|
||||||
mod = __import__(module)
|
__import__(module)
|
||||||
parts = module.split(".")
|
mod = sys.modules[module]
|
||||||
for p in parts[1:]:
|
app = eval(obj, mod.__dict__)
|
||||||
mod = getattr(mod, p, None)
|
|
||||||
if mod is None:
|
|
||||||
raise ImportError("Failed to import: %s" % module)
|
|
||||||
app = getattr(mod, obj, None)
|
|
||||||
if app is None:
|
if app is None:
|
||||||
raise ImportError("Failed to find application object: %r" % obj)
|
raise ImportError("Failed to find application object: %r" % obj)
|
||||||
if not callable(app):
|
if not callable(app):
|
||||||
@ -237,4 +234,4 @@ def daemonize():
|
|||||||
|
|
||||||
os.open(REDIRECT_TO, os.O_RDWR)
|
os.open(REDIRECT_TO, os.O_RDWR)
|
||||||
os.dup2(0, 1)
|
os.dup2(0, 1)
|
||||||
os.dup2(0, 2)
|
os.dup2(0, 2)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user