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

Krz*_*ski 22 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]
Run Code Online (Sandbox Code Playgroud)

并运行:

uwsgi --http :9090 --wsgi-file test_uwsgi.py  -???? config_file # argument for wsgi script
Run Code Online (Sandbox Code Playgroud)

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

rob*_*rto 29

python args:

--pyargv"foo bar"

sys.argv
['uwsgi', 'foo', 'bar']
Run Code Online (Sandbox Code Playgroud)

uwsgi选项:

--set foo = bar

uwsgi.opt['foo']
'bar'
Run Code Online (Sandbox Code Playgroud)

  • 你的`sys.argv`不应该是`['uwsgi','foo','bar']`? (5认同)

Tom*_*eba 5

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

[uwsgi]
wsgi-file=/path/to/test_uwsgi.py
pyargv=human
Run Code Online (Sandbox Code Playgroud)

然后让我们创建一个 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')]
Run Code Online (Sandbox Code Playgroud)

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

 uwsgi --ini /path/to/uwsgi.ini --http :8080
Run Code Online (Sandbox Code Playgroud)

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

$ curl http://localhost:8080
Hello human
Run Code Online (Sandbox Code Playgroud)

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

pyargv=-y /config.yml
Run Code Online (Sandbox Code Playgroud)