Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass command line parameters to uwsgi script

Tags:

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)

like image 415
Krzysztof Rosiński Avatar asked Feb 10 '14 18:02

Krzysztof Rosiński


People also ask

What is Uwsgi_pass?

uWSGI is an open source software application that "aims at developing a full stack for building hosting services". It is named after the Web Server Gateway Interface (WSGI), which was the first plugin supported by the project.

Why we use command line arguments in Python?

Python exposes a mechanism to capture and extract your Python command line arguments. These values can be used to modify the behavior of a program. For example, if your program processes data read from a file, then you can pass the name of the file to your program, rather than hard-coding the value in your source code.


2 Answers

python args:

--pyargv "foo bar"

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

uwsgi options:

--set foo=bar

uwsgi.opt['foo'] 'bar' 
like image 63
roberto Avatar answered Sep 30 '22 13:09

roberto


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 
like image 35
Tom Saleeba Avatar answered Sep 30 '22 13:09

Tom Saleeba