The shellescape Python module defines the shellescape.quote() function that returns a shell-escaped version of a Python string. This is a backport of the shlex.quote() function from Python 3.8 that makes it accessible to users of Python 3 versions < 3.3 and all Python 2.x versions.
quote(s)
From the Python documentation:
Return a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line, for cases where you cannot use a list.
This idiom would be unsafe:
>>> filename = 'somefile; rm -rf ~'
>>> command = 'ls -l {}'.format(filename)
>>> print(command) # executed by a shell: boom!
ls -l somefile; rm -rf ~
quote() lets you plug the security hole:
>>> command = 'ls -l {}'.format(quote(filename))
>>> print(command)
ls -l 'somefile; rm -rf ~'
>>> remote_command = 'ssh home {}'.format(quote(command))
>>> print(remote_command)
ssh home 'ls -l '"'"'somefile; rm -rf ~'"'"''
The quoting is compatible with UNIX shells and with shlex.split():
shellescape
Description
The shellescape Python module defines the
shellescape.quote()function that returns a shell-escaped version of a Python string. This is a backport of theshlex.quote()function from Python 3.8 that makes it accessible to users of Python 3 versions < 3.3 and all Python 2.x versions.quote(s)
From the Python documentation:
Return a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line, for cases where you cannot use a list.
This idiom would be unsafe:
quote()lets you plug the security hole:The quoting is compatible with UNIX shells and with
shlex.split():Usage
Include
shellescapein your project setup.py fileinstall_requiresdependency definition list:Then import the
quotefunction into your module(s) and use it as needed:License
LICENSE