python中shell执行知识点


Posted in Python onMay 06, 2020

os.system

system方法会创建子进程运行外部程序,方法只返回外部程序的运行结果。这个方法比较适用于外部程序没有输出结果的情况。

import os
os.system('ls')

commands.getstatusoutput

使用commands模块的getoutput方法,这种方法同popend的区别在于popen返回的是一个文件句柄,而本方法将外部程序的输出结果当作字符串返回,很多情况下用起来要更方便些。
主要方法:

  • commands.getstatusoutput(cmd) 返回(status, output)
  • commands.getoutput(cmd) 只返回输出结果
  • commands.getstatus(file) 返回ls -ld file的执行结果字符串,调用了getoutput,不建议使用此方法

当需要得到外部程序的输出结果时,本方法非常有用。比如使用urllib调用Web API时,需要对得到的数据进行处理。os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 如a=os.popen(cmd).read()

import os
ls = os.popen('ls')
print ls.read()

commands.getstatusoutput

使用commands模块的getoutput方法,这种方法同popend的区别在于popen返回的是一个文件句柄,而本方法将外部程序的输出结果当作字符串返回,很多情况下用起来要更方便些。
主要方法:

  • commands.getstatusoutput(cmd) 返回(status, output)
  • commands.getoutput(cmd) 只返回输出结果
  • commands.getstatus(file) 返回ls -ld file的执行结果字符串,调用了getoutput,不建议使用此方法
import commands
commands.getstatusoutput('ls -lt')   # 返回(status, output)

subprocess.call

根据Python官方文档说明,subprocess模块用于取代上面这些模块。有一个用Python实现的并行ssh工具—mssh,代码很简短,不过很有意思,它在线程中调用subprocess启动子进程来干活。

from subprocess import call
call(["ls", "-l"])
import shlex, subprocess
def shell_command(cmd, timeout) :
  data = {"rc":False, "timeout":False, "stdout":"", "stderr":""}
  try :
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    try:
      outs, errs = process.communicate(timeout=timeout)
      data["stdout"] = outs.decode("utf-8") 
      data["stderr"] = errs.decode("utf-8") 
      data["rc"] = True

    except subprocess.TimeoutExpired :
      process.kill()
      outs, errs = process.communicate()
      data["rc"] = False 
      data["stdout"] = outs.decode("utf-8") 
      data["stderr"] = "timeout"
      data["timeout"] = True 

  except Exception as e :
    data["rc"] = False 
    data["stderr"] = e 

  finally : 
    return data

到此这篇关于python中shell执行知识点的文章就介绍到这了,更多相关python shell 执行内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python性能优化的20条建议
Oct 25 Python
详解Python验证码识别
Jan 25 Python
python 网络爬虫初级实现代码
Feb 27 Python
利用标准库fractions模块让Python支持分数类型的方法详解
Aug 11 Python
浅谈pandas中Dataframe的查询方法([], loc, iloc, at, iat, ix)
Apr 10 Python
Python格式化输出字符串方法小结【%与format】
Oct 29 Python
pytorch模型存储的2种实现方法
Feb 14 Python
python编程进阶之异常处理用法实例分析
Feb 21 Python
使用PyQt的QLabel组件实现选定目标框功能的方法示例
May 19 Python
Python使用20行代码实现微信聊天机器人
Jun 05 Python
Python 解析简单的XML数据
Jul 24 Python
用python进行视频剪辑
Nov 02 Python
Python 通过监听端口实现唯一脚本运行方式
May 05 #Python
python2.7使用scapy发送syn实例
May 05 #Python
python 使用raw socket进行TCP SYN扫描实例
May 05 #Python
Python之变量类型和if判断方式
May 05 #Python
Python实现CAN报文转换工具教程
May 05 #Python
python TCP包注入方式
May 05 #Python
python构造IP报文实例
May 05 #Python
You might like
php数组删除元素示例
2014/03/21 PHP
PHP 中常量的知识整理
2017/04/14 PHP
YII框架http缓存操作示例
2019/04/29 PHP
laravel5.5添加echarts实现画图功能的方法
2019/10/09 PHP
TP5框架页面跳转样式操作示例
2020/04/05 PHP
jQuery 1.8 Release版本发布了
2012/08/14 Javascript
SwfUpload在IE10上不出现上传按钮的解决方法
2013/06/25 Javascript
深入理解javascript构造函数和原型对象
2014/09/23 Javascript
javascript文本模板用法实例
2015/07/31 Javascript
jquery性能优化高级技巧
2015/08/24 Javascript
JS日期加减,日期运算代码
2015/11/05 Javascript
jquery操作select元素和option的实例代码
2016/02/03 Javascript
JS获取checkbox的个数简单实例
2016/08/19 Javascript
ajax异步请求详解
2017/01/06 Javascript
Angular2里获取(input file)上传文件的内容的方法
2017/09/05 Javascript
PHP 实现一种多文件上传的方法
2017/09/20 Javascript
Vue中正确使用jQuery的方法
2017/10/30 jQuery
D3.js实现简洁实用的动态仪表盘的示例
2018/04/04 Javascript
通过angular CDK实现页面元素拖放的步骤详解
2020/07/01 Javascript
[02:43]DOTA2英雄基础教程 德鲁伊
2014/01/13 DOTA
python re模块的高级用法详解
2018/06/06 Python
使用Python3+PyQT5+Pyserial 实现简单的串口工具方法
2019/02/13 Python
python自定义函数实现最大值的输出方法
2019/07/09 Python
Tensorflow实现酸奶销量预测分析
2019/07/19 Python
Flask教程之重定向与错误处理实例分析
2019/08/01 Python
python中如何进行连乘计算
2020/05/28 Python
Keras之自定义损失(loss)函数用法说明
2020/06/10 Python
Idea安装python显示无SDK问题解决方案
2020/08/12 Python
Origins加拿大官网:雅诗兰黛集团高端植物护肤品牌
2017/11/19 全球购物
自荐信如何“自荐”
2013/10/24 职场文书
安全生产演讲稿
2014/05/09 职场文书
2016入党积极分子党校培训心得体会
2016/01/06 职场文书
品德与社会教学反思
2016/02/24 职场文书
PHP实现考试倒计时功能代码
2021/04/16 PHP
Java实战之用Swing实现通讯录管理系统
2021/06/13 Java/Android
CSS布局之浮动(float)和定位(position)属性的区别
2021/09/25 HTML / CSS