shell转义字符串,以便在shell命令中安全地将其用作标记(python versions 2.x&<3.3的python shlex.quote的backport)

shellescape的Python项目详细描述


源存储库:https://github.com/chrissimpkins/shellescape

说明

shellescape python模块定义了shellescape.quote()函数,该函数返回python字符串的shell转义版本。这是来自python 3.4.3的shlex.quote()函数的一个后台端口,使用户可以访问python3版本<;3.3和所有python 2.x版本。

报价

来自python文档:

返回字符串s的shell转义版本。返回的值是一个字符串,可以安全地用作shell命令行中的一个标记,用于无法使用列表的情况。

这个成语不安全:

>>>filename='somefile; rm -rf ~'>>>command='ls -l {}'.format(filename)>>>print(command)# executed by a shell: boom!ls-lsomefile;rm-rf~

quote()允许您插入安全漏洞:

>>>command='ls -l {}'.format(quote(filename))>>>print(command)ls-l'somefile; rm -rf ~'>>>remote_command='ssh home {}'.format(quote(command))>>>print(remote_command)sshhome'ls -l '"'"'somefile; rm -rf ~'"'"''

引用与unix shell和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 ~']

用法

在project setup.py文件中包含shellescape依赖项定义列表install_requires

setup(...install_requires=['shellescape'],...)

然后将quote函数导入模块,并根据需要使用它:

#!/usr/bin/env python# -*- coding: utf-8 -*-fromshellescapeimportquotefilename="somefile; rm -rf ~"escaped_shell_command='ls -l {}'.format(quote(filename))

问题报告

问题报告可在GitHub repository

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java结合两个Date实例使用Joda创建datetime   带有POI和FileOutputStream的java Swing应用程序性能问题   Java中是否存在类似于Python的异常?   Java中有界通配符的泛型   使用字符和子字符串的java不同输出   在Java中获取LDAP模式   java自定义类在自身上生成ClassNotFoundException   java连接到从internet到通过LAN连接到internet的系统的serversocket   java如何通过maven在jaxws中使用jaxb插件?   java Kerberos如何获取主体或客户端名称?   java应用服务器中是否有JDBC的单点登录解决方案?   java是存储矩阵值以便以后访问的更好方法   Java等级计算器。开始这门课我需要一些帮助   未找到Android“libc++\u shared.so”中的java OpenCV实现   当我使用web配置部署描述符时使用java。xml。它显示了错误   java为什么我的ArrayList的add方法冲突,如何编辑我的自定义add方法   java创建一个以集合为键的映射?   java如何将用户输入到数字三角形中   为什么Java在读取LinkedHashMap时会自动从字符串转换为整数   类动态创建和读取java类