2014-02-10 178 views
15

我想传递参数给一个例子WSGI应用:传递命令行参数uwsgi脚本

config_file = sys.argv[1] 

def application(env, start_response): 
    start_response('200 OK', [('Content-Type','text/html')]) 
    return [b"Hello World %s" % config_file] 

并运行:

uwsgi --http :9090 --wsgi-file test_uwsgi.py -???? config_file # argument for wsgi script 

任何聪明的办法我能做到吗?无法在uwsgi文档中找到它。也许有另一种方式为wsgi应用程序提供一些参数? (ENV变量超出范围)

回答

22

蟒蛇ARGS:

--pyargv “富巴”

sys.argv 
['uwsgi', 'foo', 'bar'] 

uwsgi选项:

--set富=酒吧

uwsgi.opt['foo'] 
'bar' 
+5

应该不是你的'sys.argv'是'[“uwsgi”, 'foo','bar']'? –

2

我最终使用的环境变量,但它设置一个启动脚本中:

def start(uwsgi_conf, app_conf, logto): 
    env = dict(os.environ) 
    env[TG_CONFIG_ENV_NAME] = app_conf 
    command = ('-c', uwsgi_conf, '--logto', logto,) 
    os.execve(os.path.join(distutils.sysconfig.get_config_var('prefix'),'bin', 'uwsgi'), command, env) 
2

您可以使用@roberto提到的pyargv设置.ini文件。让我们把我们的配置文件uwsgi.ini和使用内容:

[uwsgi] 
wsgi-file=/path/to/test_uwsgi.py 
pyargv=human 

然后让我们创建一个WGSI应用程序进行测试:

import sys 
def application(env, start_response): 
    start_response('200 OK', [('Content-Type','text/html')]) 
    return [str.encode("Hello " + str(sys.argv[1]), 'utf-8')] 

你可以看到如何加载该文件https://uwsgi-docs.readthedocs.io/en/latest/Configuration.html#loading-configuration-files

uwsgi --ini /path/to/uwsgi.ini --http :8080 

然后当我们curl的应用程序,我们可以看到我们的参数回显:

$ curl http://localhost:8080 
Hello human 

如果你想argparse风格参数传递给你的WSGI应用程序,他们在.ini也工作得很好:

pyargv=-y /config.yml 
相关问题