Python学习---重点模块之subprocess

subprocess是用来执行系统程序,查看系统的模块,

查看当前目录

第一种方法:

import subprocess
# subprocess会单独自己开辟一个线程,内部是多线程
# stdout=subprocess.PIPE 利用管道将输出的内容输出由子进程转换到主进程
obj = subprocess.Popen('dir', shell=True, stdout=subprocess.PIPE)
print(obj)         # <subprocess.Popen object at 0x00000000009A7B00>
print(str(obj.stdout.read(), 'gbk'))   # 编码用GBK,默认在当前文件所在的文件路径

image

第二种更简单:

import subprocess
print(subprocess.getoutput('dir'))

image

第三种:利用os.system()

import os
print(os.system('dir'))

 

执行某个脚本/命令

# 方法一
tracker_obj = subprocess.Popen("%s %s" % (session_tracker_script, md5_str), shell=True,
                                                           stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                                           # 这个cwd命名式指定python运行的路径的
                                                           cwd=settings.BASE_DIR)
# 方法二: 根Popen的区别是,run可以让程序在后台运行
ssh_instance = subprocess.run(login_cmd, shell=True)

subprocess的cwd的参数使用:
>>> import subprocess
>>> subprocess.run("pwd")
/home/omc/CityHunter
CompletedProcess(args='pwd', returncode=0)
>>> subprocess.run("pwd", cwd="/tmp")
/tmp
CompletedProcess(args='pwd', returncode=0)

  

 


Python下subprocess的交互

1>Python下的交互之subprocess.Popen

omc@omc-virtual-machine:~/sshpass-1.06$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>import subprocess
>>>subprocess.Popen("sshpass -p FTL200@HHH ssh root@192.168.25.133 -o 'StrictHostKeyChecking no'", shell=True, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE)       【远程交互】

>>> subprocess.Popen("df -h", shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
<subprocess.Popen object at 0x7f93b22d10f0>       【本机的命令执行】
>>> Filesystem      Size  Used Avail Use% Mounted on
udev            472M     0  472M   0% /dev
tmpfs            99M  6.2M   93M   7% /run
/dev/sda1        14G  4.9G  8.2G  38% /
tmpfs           492M  188K  492M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           492M     0  492M   0% /sys/fs/cgroup
tmpfs            99M   28K   99M   1% /run/user/108
tmpfs            99M     0   99M   0% /run/user/1000     
从上可以看出,subprocess.Popen并不能实现远程机器的实时交互,所有的输出信息都输入到了管道里,需要手动去读取内容,此时就无法实现输入信息了,所以实时交互用subprocess.Popen不行;但却可以达到本机的命令解析的效果。

 

2>Python下的交互之subprocess.run

omc@omc-virtual-machine:~/sshpass-1.06$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>>subprocess.run("sshpass -p FTL200@HH ssh root@192.168.25.133 -o 'StrictHostKeyChecking no'", shell=True)
Last login: Mon May  7 07:01:56 2018 from 192.168.25.110
[root@localhost ~]# ifconfig|grep eth2
eth2      Link encap:Ethernet  HWaddr 00:0C:29:E4:35:5D  

 

总结一下:python直接切换Linux下的shell可以用Python下的subprocess的run()来实现实时的交互。【超牛逼】

posted @ 2018-07-28 20:58  小a玖拾柒  阅读(1722)  评论(0编辑  收藏  举报