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

5

我正在从我的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
再次尝试使用check_output,但确保第一个参数是列表,就像对于call一样。仅提供字符串可能会导致异常结果。 - Kevin
只需使用http://pycurl.sourceforge.net。 - Łukasz Rogalski
在Python中,您无需使用curl来进行HTTP POST请求,例如out = urllib2.urlopen(url, data, headers={'content-type': '...'}).read()。Python中有SOAP客户端,例如suds,但我将尝试发送/接收裸xml(以解决SOAP堆栈之间的各种不兼容性)- 您可以使用xml.etree.cElementTree来解析它。 - jfs
1个回答

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


谢谢@Padraic,它有效。我如何解析从此调用获取的响应中的SOAP消息? 还假设调用成功,有没有办法解析我将从响应中收到的SOAP消息? - Ishu Gupta
如果调用没有返回0退出状态,你将会得到一个CalledProcessError错误。如果你想存储输出,只需将其存储在变量 out = check_output... 中即可。 - Padraic Cunningham

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接