我有两个模块,一个被称为tnspingexecutor.py
主要是我正在调用以 ping Oracle 实例的子进程,另一个被称为principal.py
只有主方法。的代码tnspingexecutor.py
:
import sys
import os
import re
from subprocess import Popen, PIPE
class TnsPingExecutor:
def __init__(self, instance):
self.instance = instance
def tnsping(self):
# Creating Command String. Example : "tnsping data-source-name"
command_string = "tnsping {d}".format(d=self.instance)
command = command_string.split() # Converts tnsping command list
process = Popen(command, stdout=PIPE, stderr=PIPE) # Trigger Command
self.stdout, self.stderr = process.communicate() # Stores stdout and stderr of command execution
# Stores tnsping exit code.
# Code will be 0 if tnsping succeeds.
self.status_code = process.returncode
return self.status_code, self.stdout, self.stderr
if __name__ == "__main__":
# Command line input for data source name to be checked
data_source_name = "ABCD01" #sys.argv[1]
tnspingsource = TnsPingExecutor(data_source_name)
tnspingsource.tnsping()
# Print TNSPING Command Execution Output
if tnspingsource.status_code == 0:
print("It worked! Yeah!")
else:
print("TNSPING ",data_source_name," has failed !")
当我运行python3 tnspingexecutor.py
它执行没有任何问题
tnspingsource.status_code = 0
。
当我评论时:
'''
if __name__ == "__main__":
# Command line input for data source name to be checked
data_source_name = "ABCD01" #sys.argv[1]
tnspingsource = TnsPingExecutor(data_source_name)
tnspingsource.tnsping()
# Print TNSPING Command Execution Output
if tnspingsource.status_code == 0:
print("It worked! Yeah!")
else:
print("TNSPING ",data_source_name," has failed !")
'''
然后,在principal.py
模块中:
import sys
import os
from tnspingexecutor import TnsPingExecutor
def main():
data_source_name = "ABCD01"
tnspingsource = TnsPingExecutor(data_source_name) #constructor
tnspingsource.tnsping()
if tnspingsource.status_code == 0 :
print("ping succed")
exit(1)
else:
print("ping did not succed")
exit(1)
if __name__ == "__main__":
main()
现在,当我运行python3 principal.py
它不起作用tnspingsource.status_code = 1
而不是0
我究竟做错了什么 ?