用法:
shlex.quote(s)
返回字符串
s
的 shell-escaped 版本。返回值是一个字符串,可以安全地用作 shell 命令行中的一个标记,用于无法使用列表的情况。警告
shlex
模块是只为 Unix shell 设计.quote()
函数不能保证在非 POSIX 兼容 shell 或来自其他操作系统(如 Windows)的 shell 上正确。在此类 shell 上执行此模块引用的命令可能会导致命令注入漏洞。考虑使用通过列表传递命令参数的函数,例如
subprocess.run()
和shell=False
。这个成语是不安全的:
>>> filename = 'somefile; rm -rf ~' >>> command = 'ls -l {}'.format(filename) >>> print(command) # executed by a shell: boom! ls -l somefile; rm -rf ~
quote()
让您堵住安全漏洞:>>> from shlex import quote >>> 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 ~'"'"''
引用与 UNIX shell 和
split()
兼容:>>> from shlex import 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 ~']
3.3 版中的新函数。
相关用法
- Python shlex.join用法及代码示例
- Python shutil.copyfile()用法及代码示例
- Python shutil.unregister_unpack_format()用法及代码示例
- Python shutil.get_terminal_size()用法及代码示例
- Python shutil.get_archive_formats()用法及代码示例
- Python shutil.which()用法及代码示例
- Python shutil.copytree()用法及代码示例
- Python shutil.copymode()用法及代码示例
- Python shutil.unpack_archive()用法及代码示例
- Python shutil.move()用法及代码示例
- Python shutil.copy2()用法及代码示例
- Python shutil.unregister_archive_format()用法及代码示例
- Python Wand sharpen()用法及代码示例
- Python shutil.chown()用法及代码示例
- Python shutil.copyfileobj()用法及代码示例
- Python shutil.copystat()用法及代码示例
- Python Wand shade()用法及代码示例
- Python shutil.copy()用法及代码示例
- Python shutil.get_unpack_formats()用法及代码示例
- Python shutil.disk_usage()用法及代码示例
注:本文由纯净天空筛选整理自python.org大神的英文原创作品 shlex.quote。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。