python - python异常处理不适用于fabric.api
问题描述
我正在编写一个代码,以便它可以处理来自 的错误fabric.local
,但是它总是会因错误而中止并且永远不会进入except
阻塞状态。这是我的代码,希望能从你们那里得到一些想法
此代码段正在尝试获取 Vagrant ssh 端口,如果 vagrant 未启动,请将其启动
def findsshport():
with settings(warn_only=True):
try:
print 'greping port'
return (local('vagrant ssh-config {} | grep Port'.format(env.vmId), capture=True))
except:
print 'vagrant not up'
with lcd('%s' % (buildfolder)):
local('vagrant up ext4')
return (local('vagrant ssh-config {} | grep Port'.format(env.vmId), capture=True))
env.user = 'root'
sshPort = findsshport()
env.hosts = ['127.0.0.1:' + sshPort.split()[1]]
错误
[localhost] local: vagrant ssh-config 22921a7 | grep Port
Warning: local() encountered an error (return code 1) while executing 'vagrant ssh-config 22921a7 | grep Port'
Traceback (most recent call last):
File "/home/testing/local/lib/python2.7/site-packages/test123/fabriclogin.py", line 114, in sshlogin
env.hosts = ['127.0.0.1:' + sshPort.split()[1]]
AttributeError: 'NoneType' object has no attribute 'split'
更新 类似的问题和答案
解决方案
似乎这只是来自面料的警告。我知道如果您在 ssh 上遇到错误,它不会“转换”为 Python 错误,这就是异常块不起作用的原因。请提供错误跟踪以供进一步分析。
推荐阅读
- java - E/Volley: com.android.volley.ParseError: org.json.JSONException: Value {"results":[{
- bash - Bash 和 Zsh 提示响铃并显示最后一个命令的错误代码
- encryption - 如何使用标志-nosalt -base64和-md sha256解密openssl加密文件?
- image - 在 Hybris 中安装/设置图像魔法?
- c# - MultiBinding to Value 在 WPF .Net CORE 3 中不起作用
- r - 如何在Rcpp中的n之间创建k个元素的组合?
- r - 运行碎石图时,“plot.window(...) 中的错误:需要有限的 'ylim' 值”是什么意思
- r - 如何用`with`函数调用`eval`?
- flutter - 删除所有类型:颤振中的“deviceStopped”
- docker - Kubernetes DaemonSet Permission Denied on Mounted Volume - Docker in Docker dind