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:
Paul J. Davis 2010-07-08 00:09:27 -04:00
parent 80f4d17122
commit 3ad7c1b395
2 changed files with 30 additions and 8 deletions

25
examples/alt_spec.py Normal file
View 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

View File

@ -10,6 +10,7 @@ import os
import pkg_resources
import resource
import socket
import sys
import textwrap
import time
@ -174,13 +175,9 @@ def import_app(module):
module, obj = module, "application"
else:
module, obj = parts[0], parts[1]
mod = __import__(module)
parts = module.split(".")
for p in parts[1:]:
mod = getattr(mod, p, None)
if mod is None:
raise ImportError("Failed to import: %s" % module)
app = getattr(mod, obj, None)
__import__(module)
mod = sys.modules[module]
app = eval(obj, mod.__dict__)
if app is None:
raise ImportError("Failed to find application object: %r" % obj)
if not callable(app):
@ -237,4 +234,4 @@ def daemonize():
os.open(REDIRECT_TO, os.O_RDWR)
os.dup2(0, 1)
os.dup2(0, 2)
os.dup2(0, 2)