使用Python的subprocess模块和communicate()方法时如何检索退出代码?
相关代码:
import subprocess as sp
data = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE).communicate()[0]
Run Code Online (Sandbox Code Playgroud)
我应该这样做吗?
这可能是一个愚蠢的问题,但我是git的新手,我看到一个不再存在的远程分支.
$ git branch -a
* master
remotes/origin/master
remotes/origin/production
Run Code Online (Sandbox Code Playgroud)
我不相信生产分支存在远程,也无法弄清楚为什么它仍然在本地显示.如何删除/删除此分支?这是尝试删除它的样子:
$ git push origin :production
error: unable to push to unqualified destination: production
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to 'git@IP:puppet.git'
Run Code Online (Sandbox Code Playgroud)
我可以检查所谓的远程生产分支,但得到这个:
$ git checkout origin/production
Note: checking out 'origin/production'.
You are in 'detached HEAD' state. You can look around, make experimental …Run Code Online (Sandbox Code Playgroud) 使用python的optparse模块我想在常规使用输出下面添加额外的示例行.我当前的help_print()输出如下所示:
usage: check_dell.py [options]
options:
-h, --help show this help message and exit
-s, --storage checks virtual and physical disks
-c, --chassis checks specified chassis components
Run Code Online (Sandbox Code Playgroud)
我想在我的工作中包含较少*nix识字用户的用法示例.像这样的东西:
usage: check_dell.py [options]
options:
-h, --help show this help message and exit
-s, --storage checks virtual and physical disks
-c, --chassis checks specified chassis components
Examples:
check_dell -c all
check_dell -c fans memory voltage
check_dell -s
Run Code Online (Sandbox Code Playgroud)
我怎么做到这一点?optparse选项允许哪些选项?当前代码:
import optparse
def main():
parser = optparse.OptionParser()
parser.add_option('-s', '--storage', action='store_true', default=False, help='checks virtual and physical disks') …Run Code Online (Sandbox Code Playgroud) 我在CentOS 5上有Vim 7(增强版),它附带了所有常用的Vim插件/脚本.
$ find /usr/share/vim/vim70/ -name \*python\*
/usr/share/vim/vim70/syntax/python.vim
/usr/share/vim/vim70/ftplugin/python.vim
/usr/share/vim/vim70/indent/python.vim
/usr/share/vim/vim70/autoload/pythoncomplete.vim
Run Code Online (Sandbox Code Playgroud)
我认为当打开以.py(vim file.py)结尾的文件时,它会自动加载这些插件,但我不确定是这种情况.我想要的是:
按下TAB并接收四个空格.套房,条件等的自动缩进下一行
我通过在.vimrc文件中明确设置tabstop,shiftwidth等来实现这一点.这不是上面的Python文件的用途吗?为什么我要把这些东西放在我身上.vimrc呢?我如何从Vim插件中获取这些功能呢?
目前的.vimrc:
syntax on
set hls
set expandtab
set textwidth=0
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set backspace=indent,eol,start
set incsearch
set ignorecase
set ruler
set wildmenu
set smarttab
filetype indent on
filetype on
filetype plugin on
Run Code Online (Sandbox Code Playgroud) 我正在创建一个简单的bash脚本来下载和安装python Nagios插件.在一些较旧的服务器上,脚本可能需要安装子进程模块,因此我需要确保安装了正确的python-devel文件.
什么是检查这些文件的适当的跨平台方法.想远离rpm或apt.
如果你能告诉我如何在python中进行检查,那将是有效的.谢谢!
更新:
这是我提出的最好的.谁知道更好或更确定的方法?
if [ ! -e $(python -c 'from distutils.sysconfig import get_makefile_filename as m; print m()') ]; then echo "Sorry"; fi
Run Code Online (Sandbox Code Playgroud) 我需要创建一个临时目录,它将容纳另一个命名目录和子文件.最后,命名目录和子文件将附加到tarball,并且可以删除临时目录.最初是要使用mkdtemp,但看起来TemporaryDirectory方法会自行删除?有人可以解释这些差异.
我经常使用"ct- $ char"改变直接字符和"dt- $ char",并希望能够反过来做同样的事情.
一个例子:
cobbler reposync --only=puppetlabs-6Server-x86_64
Run Code Online (Sandbox Code Playgroud)
如果我的光标位于该行的末尾,我想向后删除= char.带来这个的是我想从BASH emacs模式转到vi模式.
我正在尝试检索.lua文件中的某些字段.最初我以为我可以用逗号分开,但第二组大括号会破坏它.一个例子:
return {
{ 6163, 0, "tv", false, {1302}, "ESPN Deportes", "ESPN Deportes es el", nil,"tv","936",nil,"4x3", mediaRestrictions={"m2g" } },
{ 57075, 0, "tv", false, {1302}, "Video Rola", "Video \"Música Para Tus Ojos\", uedes ver.", nil,"tv","948",nil,"4x3", mediaRestrictions={"m2g" } },
{ 717242, 0, "tv", false, {1302,1301,1288}, "Hits", "asdlfj", nil,"cliplinear","6310",nil,"4x3", mediaRestrictions={"m2g" } },
{ 122719, 0, "tv", false, {1302,1301,1288}, "Bombone", "asdf", nil,"tv","74",nil,"4x3", mediaRestrictions={"m2g" } },
}
所以我会从第一行寻找以下内容:"ESPN Deportes"(第6场),电视(第9场),936(第10场)
上帝帮助我...或者更可能是一个stackoverflow忍者.(蟒蛇)
由S.Mark慷慨提供的解决方案:
res = conn.getresponse()
data = res.read()
# Hackisly transform the lua into …Run Code Online (Sandbox Code Playgroud) 我试图通过ssh解压多个tar文件:
ssh user@hostname "cat /dir/file*.tgz" | tar xvzf -
Run Code Online (Sandbox Code Playgroud)
以上仅适用于远程服务器上的第一个文件匹配.本地(dest)服务器只接收一个文件.已验证通配符匹配多个文件.
还有另一种方法吗?
我似乎无法在机架中间件的预处理部分中附加到 ARGV。有人知道当中间件存在时,机架如何处理 ARGV 吗?如何在我的中间件中修改 ARGV?必须有办法做到这一点,我完全不知所措。
ARGV << "--debug"
ARGV << "--host" << "localhost"
class Pre
def initialize(app)
@app = app
end
def call(env)
if some_env_related_logic
ARGV << "--test"
end
@app.call(env)
end
end
require 'Somecommand'
use Pre
run Somecommand
Run Code Online (Sandbox Code Playgroud)
现在,“--test”参数不会添加到 Somecommand 中可用的 ARGV 中。
更新:
看来 Somecommand 应用程序在初始化方法中使用了 ARGV。这发生在第一个中间件之前。所以现在的问题是:如何创建一个调用第二个机架应用程序的机架应用程序?因为我需要在秒机架应用程序实例化之前评估 ENV。
刚开始使用python并且知道足够知道我什么都不知道.我想找到将列表拆分为dicts列表的替代方法.示例列表:
data = ['ID:0:0:0',
'Status:Ok',
'Name:PhysicalDisk0:0:0',
'State:Online',
'FailurePredicted:No',
'ID:0:0:1',
'Status:Ok',
'Name:PhysicalDisk0:0:1',
'State:Online',
'FailurePredicted:No']
Run Code Online (Sandbox Code Playgroud)
完成的dicts列表:
[{'Status': 'Ok',
'State': 'Online',
'ID': '0:0:0',
'FailurePredicted': 'No',
'Name': 'PhysicalDisk0:0:0'},
{'Status': 'Ok',
'State': 'Online',
'ID': '0:0:1',
'Name': 'PhysicalDisk0:0:1',
'FailurePredicted': 'No'}]
Run Code Online (Sandbox Code Playgroud)
该列表具有需要多个dicts的重复元素,并且列表的长度不同.我的代码似乎可以简化,只要我更了解Python.我目前的代码:
删除的代码它没有用.:(
----------- File output as requested -------------------
# omreport storage pdisk controller=0
List of Physical Disks on Controller PERC 5/i Integrated (Embedded)
Controller PERC 5/i Integrated (Embedded)
ID : 0:0:0
Status : Ok
Name : Physical Disk 0:0:0
State : Online
Failure Predicted : …Run Code Online (Sandbox Code Playgroud) for host in platforms:
f = open(host, 'w')
f.write('define host {\n')
f.write(' host_name {}\n'.format(host))
f.write(' alias {}\n'.format(host))
f.write(' display_name {}\n'.format(host))
f.write(' address {}\n'.format(str(socket.gethostbyname(host))))
f.write(' use linux-server\n')
f.write(' register 1\n')
f.write('}\n')
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?是否有一种简单的方法来格式化所有这些字符串但只进行一次写入方法调用?如果以上被认为是最好的做法,那么看起来好像它可能更漂亮.
python ×8
bash ×2
vim ×2
argv ×1
dictionary ×1
git ×1
list ×1
middleware ×1
optparse ×1
parsing ×1
rack ×1
regex ×1
ruby ×1
ssh ×1
subprocess ×1
tar ×1
vi ×1
vim-plugin ×1