将命令行参数传递给uwsgi脚本

时间:2014-02-10 18:56:56

标签: python wsgi uwsgi

我正在尝试将参数传递给示例wsgi应用程序,:

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应用程序提供一些参数? (环境变量超出范围)

3 个答案:

答案 0 :(得分:28)

python args:

- pyargv“foo bar”

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

uwsgi选项:

- 设置foo = bar

uwsgi.opt['foo']
'bar'

答案 1 :(得分:3)

您可以使用@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

答案 2 :(得分:2)

我最终使用了一个env变量,但是在启动脚本中设置了它:

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)
相关问题