python中写shell(转)
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(转)的更多相关文章
- 使用RSA加密在Python中逆向shell
i春秋翻译小组-Neo(李皓伟) 使用RSA加密在Python中逆向shell 这是一个关于使用RSA加密编程逆向shell的python教程. 我想提一下,这篇文章更多的是关于理解shell中涉及的 ...
- python中执行shell的两种方法总结
这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...
- python中执行shell命令的几个方法小结(转载)
转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...
- 转--python 中写单例
原文地址 原文地址2 Python中的单例模式的几种实现方式的及优化 阅读目录(Content) 单例模式 实现单例模式的几种方式 1.使用模块 2.使用装饰器 3.使用类 4.基于__new__方法 ...
- 【python中调用shell命令使用PIPE】使用PIPE作为stdout出现假卡死的情况——将stdout重定向为输出到临时文件
在Python中,调用:subprocess.Popen(cmd, stdout = PIPE, stderr = PIPE, shell= true)的时候,如果调用的shell命令本身在执行之后会 ...
- [Python]在python中调用shell脚本,并传入参数-02python操作shell实例
首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数 test_shell ...
- python 中写hive 脚本
1.直接执行.sql脚本 import numpy as np import pandas as pd import lightgbm as lgb from pandas import DataFr ...
- 怎样在python中写注释
python中的注释是以井号: # 开头, 一般会在#后加一个空格. # This is a comment print("Hello, World!") 多行注释的语法是三引号: ...
- Python中调用shell
1 简单调用shell命令 os.system(command) 在一个子shell中运行command命令, 并返回command命令执行完毕后的退出状态. 这实际上是使用C标准库函数system( ...
随机推荐
- 使用JavaScript+Html创建win8应用(一)
最近在学习win8 metro app的开发,今天刚刚学了一个小的例子,分享一下 开始之前你需要准备... 1.开发win8应用需要具备Windows 8 和 Microsoft Vi ...
- 嵌套回调异步与$.Deferred异步
HTML: <input type="button" id="btn1" value="嵌套回调异步"> <input t ...
- Careercup - Microsoft面试题 - 5684901156225024
2014-05-10 23:45 题目链接 原题: Arrange the numbers in an array in alternating order. For example if the a ...
- eclipse for jee版配置tomcat
在网上搜到的大多都是插件配置,其实默认的就可以配置tomcat的. 第一步:New -> Other -> Server ,然后选择Apache下的tomcat的版本. 注意:如果Next ...
- Gulp压缩JavaScript代码
因为gulp是自动化工具,所以我们得告诉它,需要怎么做,才能达到我们的目的. 我们首先得新建一个js文件,用来编写我们的需求,以便gulp能按着我们的意愿来执行. 我将这个js文件取名叫gulpfil ...
- 《我是IT一只小小鸟》读后感
<我是IT一只小小鸟>读后感 首先,非常感谢我的老师给我推荐了这么一本书,虽然刚开始因为这门课学分太低,所以我对老师布置了字数这么多的作业存在有很大的不满,但在看了这本书后我的不满立马得到 ...
- 【BZOJ】【1150】【CTSC2007】数据备份Backup
堆/贪心 一共N-1个元素……用堆维护最大值,取了第x个元素以后,插入v[x-1]+v[x+1]-v[x]这个元素,如果再取这个新元素就表示不取x,而取x-1和x+1……大概就是这种“带反悔”的思路吧 ...
- 【BZOJ】【1047】【HAOI2007】理想的正方形
DP/单调队列优化 一眼看上去就是DP 我想的naive的二维DP是酱紫滴: mx[i][j][k]表示以(i,j)为右下角的k*k的正方形区域内的最大值,mn[i][j][k]同理 mx[i][j] ...
- livevent的几个问题
关于libevent的几个问题 1.他到底是如何保证这个套接字有效的 主线程去断开关闭套接字,主线程去调用buffwrite,在子线程收到客户端断开时,主线程延迟1分钟释放,这样保证子线程操作完该so ...
- 研究Dropbox Server端文件系统
一.传统文件系统 可以理解成两部分:1.真正的storage区,被分割成n个扇区:2.文件系统,其实就是一个FAT表. 二.Dropbox的文件系统 例如,一个modeo.mov的文件,大小为15M. ...