Changeset 391 for python/trunk/Doc/library/pipes.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/pipes.rst
r2 r391 1 2 1 :mod:`pipes` --- Interface to shell pipelines 3 2 ============================================= … … 8 7 .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il> 9 8 9 **Source code:** :source:`Lib/pipes.py` 10 11 -------------- 10 12 11 13 The :mod:`pipes` module defines a class to abstract the concept of a *pipeline* … … 14 16 Because the module uses :program:`/bin/sh` command lines, a POSIX or compatible 15 17 shell for :func:`os.system` and :func:`os.popen` is required. 16 17 The :mod:`pipes` module defines the following class:18 18 19 19 … … 25 25 26 26 >>> import pipes 27 >>> t =pipes.Template()27 >>> t = pipes.Template() 28 28 >>> t.append('tr a-z A-Z', '--') 29 >>> f =t.open('/tmp/1', 'w')29 >>> f = t.open('pipefile', 'w') 30 30 >>> f.write('hello world') 31 31 >>> f.close() 32 >>> open(' /tmp/1').read()32 >>> open('pipefile').read() 33 33 'HELLO WORLD' 34 35 36 .. function:: quote(s) 37 38 .. deprecated:: 2.7 39 Prior to Python 2.7, this function was not publicly documented. It is 40 finally exposed publicly in Python 3.3 as the 41 :func:`quote <shlex.quote>` function in the :mod:`shlex` module. 42 43 Return a shell-escaped version of the string *s*. The returned value is a 44 string that can safely be used as one token in a shell command line, for 45 cases where you cannot use a list. 46 47 This idiom would be unsafe:: 48 49 >>> filename = 'somefile; rm -rf ~' 50 >>> command = 'ls -l {}'.format(filename) 51 >>> print command # executed by a shell: boom! 52 ls -l somefile; rm -rf ~ 53 54 :func:`quote` lets you plug the security hole:: 55 56 >>> command = 'ls -l {}'.format(quote(filename)) 57 >>> print command 58 ls -l 'somefile; rm -rf ~' 59 >>> remote_command = 'ssh home {}'.format(quote(command)) 60 >>> print remote_command 61 ssh home 'ls -l '"'"'somefile; rm -rf ~'"'"'' 62 63 The quoting is compatible with UNIX shells and with :func:`shlex.split`: 64 65 >>> remote_command = shlex.split(remote_command) 66 >>> remote_command 67 ['ssh', 'home', "ls -l 'somefile; rm -rf ~'"] 68 >>> command = shlex.split(remote_command[-1]) 69 >>> command 70 ['ls', '-l', 'somefile; rm -rf ~'] 34 71 35 72
Note:
See TracChangeset
for help on using the changeset viewer.