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 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():
>>> remote_command = split(remote_command)
>>> remote_command
['ssh', 'home', "ls -l 'somefile; rm -rf ~'"]
>>> command = split(remote_command[-1])
>>> command
['ls', '-l', 'somefile; rm -rf ~']
Usage
Include shellescape in your project setup.py file install_requires dependency definition list:
setup(
...
install_requires=['shellescape'],
...
)
Then import the quote function into your module(s) and use it as needed:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from shellescape import quote
filename = "somefile; rm -rf ~"
escaped_shell_command = 'ls -l {}'.format(quote(filename))
License
Release files for shellescape 3.8.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| shellescape-3.8.1.tar.gz | 5.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| shellescape-3.8.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 8.3 kB
Release files / shellescape-3.8.1.tar.gz
| Download URL | shellescape-3.8.1.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
40b310b30479be771bf3ab28bd8d40753778488bd46ea0969ba0b35038c3ec26
|
|
BLAKE2b-256 checksum How to use checksums |
194013b9e84bf04774365830cbed1bd95a989d5324a99d207bcb1619a6c517f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6
|
Release files / shellescape-3.8.1-py2.py3-none-any.whl
| Download URL | shellescape-3.8.1-py2.py3-none-any.whl |
|---|---|
| Size | 3.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
f17127e390fa3f9aaa80c69c16ea73615fd9b5318fd8309c1dca6168ae7d85bf
|
|
BLAKE2b-256 checksum How to use checksums |
d0f40081137fceff5779cd4205c1e96657e41cc2d2d56c940dc8eeb6111780f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6
|