【问题标题】:Parse the output of subprocess.call() from Python从 Python 解析 subprocess.call() 的输出
【发布时间】:2015-04-26 09:56:51
【问题描述】:

我正在调用我的 Python 代码中的网络服务:

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)

【问题讨论】:

  • 再试一次check_output,但要确保第一个参数是一个列表,就像call一样。只提供一个字符串可能会导致异常结果。
  • 您不需要curl 在 Python 中发出 http post 请求,例如 out = urllib2.urlopen(url, data, headers={'content-type': '...'}).read()。有soap clients in Python,例如suds,但我会尝试发送/接收裸xml(以解决soap-stacks之间的各种不兼容性)——你可以使用xml.etree.cElementTree来解析它。

标签: python python-2.7 subprocess


【解决方案1】:

不要 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,它有效。如何从我从这个电话得到的响应中解析肥皂消息?还假设呼叫成功,有没有办法可以解析我将从响应中获得的肥皂消息?
  • 如果调用未返回 0 退出状态,您将获得 CalledProcessError,如果您想存储输出,只需将其存储在变量 out = check_output...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多