python中写shell,亲测可用,转自stackoverflow

To run a bash script, copy from stackoverflow

def run_script(script, stdin=None):
"""Returns (stdout, stderr), raises error on non-zero return code"""
import subprocess
# Note: by using a list here (['bash', ...]) you avoid quoting issues, as the
# arguments are passed in exactly this order (spaces, quotes, and newlines won't
# cause problems):
proc = subprocess.Popen(['bash', '-c', script],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
stdout, stderr = proc.communicate()
if proc.returncode:
raise ScriptException(proc.returncode, stdout, stderr, script)
return stdout, stderr

class ScriptException(Exception):
def init(self, returncode, stdout, stderr, script):
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
Exception.init('Error in script')

run_script("shell script")

python中写shell(转)的更多相关文章

  1. 使用RSA加密在Python中逆向shell

    i春秋翻译小组-Neo(李皓伟) 使用RSA加密在Python中逆向shell 这是一个关于使用RSA加密编程逆向shell的python教程. 我想提一下,这篇文章更多的是关于理解shell中涉及的 ...

  2. python中执行shell的两种方法总结

    这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...

  3. python中执行shell命令的几个方法小结(转载)

    转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...

  4. 转--python 中写单例

    原文地址 原文地址2 Python中的单例模式的几种实现方式的及优化 阅读目录(Content) 单例模式 实现单例模式的几种方式 1.使用模块 2.使用装饰器 3.使用类 4.基于__new__方法 ...

  5. 【python中调用shell命令使用PIPE】使用PIPE作为stdout出现假卡死的情况——将stdout重定向为输出到临时文件

    在Python中,调用:subprocess.Popen(cmd, stdout = PIPE, stderr = PIPE, shell= true)的时候,如果调用的shell命令本身在执行之后会 ...

  6. [Python]在python中调用shell脚本,并传入参数-02python操作shell实例

    首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数  test_shell ...

  7. python 中写hive 脚本

    1.直接执行.sql脚本 import numpy as np import pandas as pd import lightgbm as lgb from pandas import DataFr ...

  8. 怎样在python中写注释

    python中的注释是以井号: # 开头, 一般会在#后加一个空格. # This is a comment print("Hello, World!") 多行注释的语法是三引号: ...

  9. Python中调用shell

    1 简单调用shell命令 os.system(command) 在一个子shell中运行command命令, 并返回command命令执行完毕后的退出状态. 这实际上是使用C标准库函数system( ...

随机推荐

  1. MyEclipse 中的各种有的没的快捷方式

    快捷键1 (CTRL) Ctrl+1 快速修复Ctrl+D: 删除当前行  Ctrl+Q  定位到最后编辑的地方  Ctrl+L  定位在某行   Ctrl+O  快速显示 OutLine  Ctrl ...

  2. Javascript对象的创建模式 -- 深入了解Javascript

    /* 一.模式1:命名空间(namespace) 优点:减少全局命名所需的数量,避免命名冲突或过度 */ // 更简洁的方式 var MYAPP = MYAPP || {}; //定义通用方法 MYA ...

  3. 从URL中获取搜索关键字

    public string GetSearchKeyWords(string strQuery) { string result = ""; string pattern = &q ...

  4. Codeforces Round #353 (Div. 2) E. Trains and Statistic 线段树+dp

    题目链接: http://www.codeforces.com/contest/675/problem/E 题意: 对于第i个站,它与i+1到a[i]的站有路相连,先在求所有站点i到站点j的最短距离之 ...

  5. C#读书笔记之并行任务

    这两天遇到一个多线程任务优化的问题,现在解决了,把心得用例子的形式记下来. 假设有四个任务: 任务1:登陆验证(CheckUser) 任务2:验证成功后从Web服务获取数据(GetDataFromWe ...

  6. BZOJ2039 [2009国家集训队]employ人员雇佣

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=2039 鉴于一开始看题如果不仔细是看不懂题目的,还是说一下题目大意 [题目大意]:给定n个人 ...

  7. C#异常处理策略

    1.如果某些异常,本组件无法处理,不要捕获,但要声明有这个异常.例如SecurityException. 2.如果某些异常,本组件可以处理,捕获并处理它,不再声明此异常.例如DirectoryNotF ...

  8. javascript中继承(二)-----借用构造函数继承的个人理解

    本人目录如下: 零.寒暄&回顾 一,借用构造函数 二.事件代理 三,call和apply的用法 四.总结 零.寒暄&回顾 上次博客跟大家分享了自己对原型链继承的理解,想看的同学欢迎猛击 ...

  9. XSS的原理分析与解剖(二)

    0×01 前言:  上节(http://www.freebuf.com/articles/web/40520.html)已经说明了xss的原理及不同环境的构造方法.本期来说说XSS的分类及挖掘方法. ...

  10. 比较IE6的不同之处,与IE8 IE11 比较

    文档申明为  <!DOCTYPE html> IE6或者IE特有的一些东西 1.盒子模型 IE6:(使用 !DOCTYPE 声明指定 standards-compliant 模式) mar ...