Python子进程错误管理。无法捕获非零退出状态

Python子进程错误管理。无法捕获非零退出状态,python,linux,python-2.7,subprocess,Python,Linux,Python 2.7,Subprocess,我正在使用python中的子流程模块使用sakis3g将我的3G加密狗连接到3G网络 请参见我使用的代码: check_output(['sakis3g', '--sudo', 'connect', 'OTHER="USBMODEM"', 'USBMODEM="12d1:1001"', 'APN="internet"']) 有时,我的加密狗可能会弹出一个错误:“此设备没有任何GSM功能…” 我完全可以接受它,因为它只需要一次简单的重试,而且通常可以正常工作 但是,使用子进程时,我遇到了返回非零

我正在使用python中的子流程模块使用sakis3g将我的3G加密狗连接到3G网络

请参见我使用的代码:

check_output(['sakis3g', '--sudo', 'connect', 'OTHER="USBMODEM"', 'USBMODEM="12d1:1001"', 'APN="internet"'])
有时,我的加密狗可能会弹出一个错误:“此设备没有任何GSM功能…”

我完全可以接受它,因为它只需要一次简单的重试,而且通常可以正常工作

但是,使用子进程时,我遇到了返回非零退出状态的错误
,它将使我的软件完全崩溃

因为我只需要一次重试,所以我尝试在
try:。。。除外:…
。 我试图捕获的错误是
子流程。调用的流程错误
,如果退出状态为非零,则应根据返回

但是,这似乎并没有解决问题,问题仍然存在:

Traceback (most recent call last):
  File "run.py", line 91, in <module>
    print connect_3G()
  File "run.py", line 28, in connect_3G
    check_call(['sakis3g', '--sudo', 'connect', 'OTHER="USBMODEM"', 'USBMODEM="12d1:1001"', 'APN="internet"'])
  File "/usr/lib/python2.7/subprocess.py", line 540, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['sakis3g', '--sudo', 'connect', 'OTHER="USBMODEM"', 'USBMODEM="12d1:1001"', 'APN="internet"']' returned non-zero exit status 95

我认为您做的是正确的……但是将引发异常的代码移动到
try
块中

def connect_3G():
    while True:
        try:
            check_output(['sakis3g', '--sudo', 'connect', 'OTHER="USBMODEM"', 'USBMODEM="12d1:1001"', 'APN="internet"'])
            return 'Connected to ip: {}'.format(json.loads(requests.get('http://httpbin.org/ip').content)['origin'])
        except subprocess.CalledProcessError:
            print 'Oops, problem connecting to 3G. Better retry fam.'

我认为您做的是正确的……但是将引发异常的代码移动到
try
块中

def connect_3G():
    while True:
        try:
            check_output(['sakis3g', '--sudo', 'connect', 'OTHER="USBMODEM"', 'USBMODEM="12d1:1001"', 'APN="internet"'])
            return 'Connected to ip: {}'.format(json.loads(requests.get('http://httpbin.org/ip').content)['origin'])
        except subprocess.CalledProcessError:
            print 'Oops, problem connecting to 3G. Better retry fam.'

此外,只需打印出错误,可能会帮助您调试代码:

def connect_3G():
    while True:
        try:
            check_output(['sakis3g', '--sudo', 'connect', 'OTHER="USBMODEM"', 'USBMODEM="12d1:1001"', 'APN="internet"'])
            return 'Connected to ip: {}'.format(json.loads(requests.get('http://httpbin.org/ip').content)['origin'])
        except subprocess.CalledProcessError as error:
            print 'Oops, problem connecting to 3G. Better retry fam.', error.message

此外,只需打印出错误,可能会帮助您调试代码:

def connect_3G():
    while True:
        try:
            check_output(['sakis3g', '--sudo', 'connect', 'OTHER="USBMODEM"', 'USBMODEM="12d1:1001"', 'APN="internet"'])
            return 'Connected to ip: {}'.format(json.loads(requests.get('http://httpbin.org/ip').content)['origin'])
        except subprocess.CalledProcessError as error:
            print 'Oops, problem connecting to 3G. Better retry fam.', error.message

看起来异常是由
check\u output
调用引发的,该调用不在try-except块内?@strubbly-Yep这似乎是问题所在,不敢相信我忽略了这一点!看起来异常是由
check\u output
调用引发的,该调用不在try-except块内?@strubbly-Yep这似乎是问题所在,不敢相信我忽略了这一点!Welp我不敢相信我没有看到。Welp我不敢相信我没有看到。好的想法,但这个错误的根源是我知道的,不幸的是我无法解决。好的想法,但这个错误的根源是我知道的,不幸的是我无法解决。