python 在shell中运行命令并进行错误处理。 #python #subprocess
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在shell中运行命令并进行错误处理。 #python #subprocess相关的知识,希望对你有一定的参考价值。
#!/usr/bin/python
def tryUrl(url):
try:
# check_output returns output from command so it could be printed here
subprocess.check_output('ping ' + url, shell=True)
return 1
except subprocess.CalledProcessError, e:
print 'Connection failed, error: %s' % e.output
return 0
def connectivityTestWithFail():
required = 6
attempts = 5
urls = ['http://www.google.com', 'http://www.amazon.com']
working = 0
for url in urls * attempts:
working += tryUrl(url)
if working >= required:
break
print str(working) + "\n"
#connectivityTestWithFail()
以上是关于python 在shell中运行命令并进行错误处理。 #python #subprocess的主要内容,如果未能解决你的问题,请参考以下文章
需要从 Python 脚本运行 shell 命令并存储在变量中 [重复]
shell脚本调用python处理中文错误的问题
异常处理模块包时间模块subprocess(调用shell命令)
用于包装嘈杂的python脚本并使用sed删除特定行的shell命令问题[重复]
运行无限循环并从“外部”获取命令(例如 shell 或其他脚本)
怎样才能让python 脚本像shell命令一样直接运行并可以接收参数