Pass command line parameters to uwsgi script

2019-03-17 21:34发布

I'm trying to pass arguments to an example wsgi application, :

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]

And run:

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

Any smart way I can achieve it? Couldn't find it in uwsgi docs. Maybe there is another way of providing some parameters to the wsgi application? (env. variables are out of scope)

3条回答
我只想做你的唯一
2楼-- · 2019-03-17 21:57

You could use an .ini file with the pyargv setting that @roberto mentioned. Let's call our config file uwsgi.ini and use the content:

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

Then let's create a WGSI app to test it:

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')]

You can see how to load this file https://uwsgi-docs.readthedocs.io/en/latest/Configuration.html#loading-configuration-files:

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

Then when we curl the app, we can see our param echoed back:

$ curl http://localhost:8080
Hello human

If you are trying to pass argparse style arguments to your WSGI app, they work just fine in the .ini too:

pyargv=-y /config.yml
查看更多
做自己的国王
3楼-- · 2019-03-17 22:14

python args:

--pyargv "foo bar"

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

uwsgi options:

--set foo=bar

uwsgi.opt['foo']
'bar'
查看更多
Viruses.
4楼-- · 2019-03-17 22:19

I ended up using an env variable but setting it inside a start script:

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)
查看更多
登录 后发表回答