开发者

Python Subprocess returncode - different results

开发者 https://www.devze.com 2023-02-26 18:48 出处:网络
I came to run a script on a production box, and it failed in one of it\'s first checks. I\'ve narrowed it down to the fact my \"ping\" check (invoked by subprocess) is returning a non-zero result.

I came to run a script on a production box, and it failed in one of it's first checks. I've narrowed it down to the fact my "ping" check (invoked by subprocess) is returning a non-zero result.

Here's the code in it's simplest form:

import subprocess
import sys
ip="127.0.0.1"
ping = subprocess.Popen(["ping", "-c", "2", "-w", "1", ip], shell=False)
ping.wait()
if ping.returncode != 0:
    print ping.returncode, "ERROR: failed to ping host. Please check."
    sys.exit(1)
else:
    print "OK"

Both servers are CentOS 5.5 & Python 2.4.3, but one works and one doesn't!

From the command line a manual ping (of 127.0.0.1) returns 0 in both c开发者_JAVA技巧ases. Why is their a difference when using the subprocess module in python?

Cheers.

0

精彩评论

暂无评论...
验证码 换一张
取 消