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

问题描述 投票:20回答:3

我正在尝试将参数传递给示例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应用程序提供一些参数? (环境变量超出范围)

python wsgi uwsgi
3个回答
28
投票

python args:

--pyargv“foo bar”

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

uwsgi选项:

--set foo = bar

uwsgi.opt['foo']
'bar'

3
投票

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

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

然后让我们创建一个WSGI应用程序来测试它:

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应用程序时,我们可以看到我们的param回应:

$ curl http://localhost:8080
Hello human

如果您尝试将argparse样式参数传递给WSGI应用程序,它们在.ini中也可以正常工作:

pyargv=-y /config.yml

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)
© www.soinside.com 2019 - 2024. All rights reserved.