从Python解析subprocess.call()的输出

时间:2015-02-24 16:48:36

标签: python python-2.7 subprocess

我从我的Python代码中调用Web服务:

response = subprocess.call(['curl', '-k', '-i', '-H' , 'content-type: application/soap+xml' ,'-d',  etree.tostring(tree), '-v' ,'https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1'])

该服务返回一条soap消息,如何解析soap消息并查明它是否失败或成功?

我尝试使用以下内容,但结果出错:

subprocess.check_output("curl -k --data "+etree.tostring(tree)+"@SampleRequest.xml -v  https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1",stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

1 个答案:

答案 0 :(得分:5)

不要PIPE只需致电check_output即可传递参数列表并移除shell=True

 out = subprocess.check_output(["curl", "-k","--data", etree.tostring(tree)+"@SampleRequest.xml", "-v",  "https://world-service-dev.intra.aexp.com:4414/worldservice/CLIC/CaseManagementService/V1"])

如果您获得非零退出代码,则会获得CalledProcessError