繁体   English   中英

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

[英]Pass command line parameters to uwsgi script

我正在尝试将参数传递给示例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 args:

--pyargv“foo bar”

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

uwsgi选项:

--set foo = bar

uwsgi.opt['foo']
'bar'

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

我最终使用了一个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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM