python通过http请求发送soap报文进行webservice接口调用

最近学习Python调用webservice 接口,开始的时候主要采用suds 的方式生产client调用,后来发现公司的短信接口采用的是soap报文来调用的,然后开始了谷歌,最后采用httplib 模块发送报文数据,来调用接口,下面直接来一个实例

# -*- coding: utf-8 -*-
import httplib


def mdsmssend(sn,pwd,mobile,context):
  #定义发送报文
   SoapMessage ='''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <mdsmssend xmlns="http://entinfo.cn/">
      <sn>%s</sn>
      <pwd>%s</pwd>
      <mobile>%s</mobile>
      <content>%s</content>
    </mdsmssend>
  </soap:Body>
</soap:Envelope>'''

   SoapMessage=SoapMessage %(sn,pwd,mobile,context)
   #使用的WebService地址为sdk.entinfo.cn:8061/webservice.asmx,
   webservice = httplib.HTTP("sdk.entinfo.cn:8061")
   #连接到服务器后的第一个调用。它发送由request字符串到到服务器
   webservice.putrequest("POST", "/webservice.asmx")
   webservice.putheader("Host", "sdk.entinfo.cn:8061")
   webservice.putheader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)")
   webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
   webservice.putheader("Content-length", "%d" % len(SoapMessage))
   webservice.putheader("SOAPAction", "\"http://entinfo.cn/mdsmssend\"")
   #发送空行到服务器,指示header的结束
   webservice.endheaders()
   #发送报文数据到服务器
   webservice.send(SoapMessage)
   #获取返回HTTP 响应信息
   statuscode, statusmessage, header = webservice.getreply()
   return statuscode

这样大功告成

posted on 2015-07-21 15:18  梦雨情殇  阅读(11672)  评论(1编辑  收藏  举报

导航