Several Methods for executing shell
This section describes four methods for python to execute shell commands:
1. the OS. system () function in the OS module executes shell commands.
Note: This method cannot be output by shell commands.
2. popen () # The result of executing the command is a string. You need to process it yourself to obtain the desired information.
The result is the same as that of the first method.
3. commands Module # It is convenient to obtain command output (including standard and error output) and execution status bit.
Commands. getstatusoutput (cmd) returns (status, output)
Commands. getoutput (cmd) returns only output results
Commands. getstatus (file) returns the execution result string of the ls-ld file and calls getoutput. This method is not recommended.
4. subprocess Module
The subprocess module can be used to create a new process. It can connect to the input, output, and error pipelines of the new process, and obtain the return status of the new process execution. The subprocess module is used to replace OS. system () and OS. popen.(), Commands.And other old functions or modules.
Import subprocess 1, subprocess. call (command, shell = True) # The result is printed directly. 2. subprocess. popen (command, shell = True) ### it can also be subprocess. popen (command, stdout = subprocess. PIPE, shell = True) # You can output the result.
If the command is not an executable file, shell = True cannot be omitted.
Shell = True: Execute command in shell
You can execute shell commands in all four methods.
Pysftp-friendly command execution and interaction tools
- import pysftp with pysftp.Connection('hostname', username='me', password='secret') as sftp: with sftp.cd('public'):
- # temporarily chdir to public
- sftp.put('/my/local/filename') # upload file to public/ on remote
- sftp.get('remote_file')
- # get a remote file
All methods
- dir(pysftp.Connection) ['class', 'del', 'delattr', 'dict', 'doc', 'enter', 'exit', 'format',
- 'getattribute', 'hash', 'init', 'module', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof',
- 'str', 'subclasshook', 'weakref', 'set_authentication', 'set_logging', 'set_username', 'sftp_connect', '_start_transport',
- 'active_ciphers', 'active_compression', 'cd', 'chdir', 'chmod', 'chown', 'close', 'cwd', 'execute', 'exists', 'get', 'get_d',
- 'get_r', 'getcwd', 'getfo', 'isdir', 'isfile', 'lexists', 'listdir', 'listdir_attr', 'logfile', 'lstat', 'makedirs', 'mkdir',
- 'normalize', 'open', 'put', 'put_d', 'put_r', 'putfo', 'pwd', 'readlink', 'remote_server_key', 'remove', 'rename', 'rmdir',
- 'security_options', 'sftp_client', 'stat', 'symlink', 'timeout', 'truncate', 'unlink', 'walktree']
Https://bitbucket.org/dundeemt/pysftp official link
Download https://pypi.python.org/pypi/pysftp/
Http://pysftp.readthedocs.io/en/release_0.2.9/ Documentation Center