Question

I have a flask application that takes what a user types into a form and then adds a little bit extra and passes this as variables in another script. For example:

session['search'] = '--q ' + request.args.get('search_key')

which would the run a script I have with that on the end so when user types " abc " this is passed;

example.py --q abc

what i would like to do is allow for spaces so user types " d e f " this would be passed;

example.py --q "d e f"

Is this possible to achieve? At the moment the python script example.py takes the space as moving onto next argument. Currently when i include "" the word 'search' is sent instead of what the user has put in the form.

Was it helpful?

Solution

How are you running the second script?

If you do it the right way, you won't invoke a shell at all, so you don't need to add quotes (which are used by shells to decide how to split arguments into an argv array), and instead can pass the exact argv array you want.

For instance:

subprocess.Popen(['example.py', '--q', request.args.get('search_key', '')])

No quotes added, but no quotes needed -- the result from request.args.get('search_key, '') is passed as a single argument. Note that this only works if you do not pass shell=True.

See the subprocess module documentation for details -- on collecting stdout and stderr, checking exit status, and much more.

OTHER TIPS

You can do it as follows:

in_text = '"'+str(input('Enter arg: '))+'"'
session['search'] = '--q ' + request.args.get(in_text)

Basically, it takes in the user input as a string and adds a " at the start and end:

>>> a = '"'+str(input('In: '))+'"'
In: hello
>>> a
'"hello"'
>>> print a
"hello"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top