本节将展示一种代码模板,它会以‘手动’方式向JWSDP的webservice(document/literal)传递SOAP消息。后面也会用相同的方法访问 Infobel webservice。
使用python向web服务器传送一个文件相对容易些。对于JWSDP webservice,不要忘了在消息头中增加Content-type,只有这样,调用才能被服务器正常接收。下面是一个命令行示例,SOAP响应都不加解析地被打印出来。由于返回数据相对复杂,我会在稍后的Infobel web service示例中讲解如何使用PyXml解析SOAP消息。
在接下来的示例中我们访问readLS(),它带有一个string类型的返回值。
# post方式提交 xml的 soap 消息import sys, httplib # a "as lighter as possible" soap message:SM_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><ns1:readLS xmlns:ns1="http://phonedirlux.homeip.net/types"><String_1>%s</String_1></ns1:readLS></SOAP-ENV:Body></SOAP-ENV:Envelope>""" SoapMessage = SM_TEMPLATE%("Your message or e-mail") print SoapMessage #构造并发送消息头webservice = httplib.HTTP("www.pascalbotte.be")webservice.putrequest("POST", "/rcx-ws/rcx")webservice.putheader("Host", "www.pascalbotte.be")webservice.putheader("User-Agent", "Python post")webservice.putheader("Content-type", "text/xml; charset=/"UTF-8/"")webservice.putheader("Content-length", "%d" % len(SoapMessage))webservice.putheader("SOAPAction", "/"/"")webservice.endheaders()webservice.send(SoapMessage) # 获取返回statuscode, statusmessage, header = webservice.getreply()print "Response: ", statuscode, statusmessageprint "headers: ", headerres = webservice.getfile().read()print res
如果你想使用我的webservice测试这段代码,请点击这里(如果在线)。通常,你可以使用自己喜爱的开发工具做一个客户端,并修改SOAP消息访问别的webservice,用以捕获客户端与服务端的SOAP消息交换。
如何投递(POST)动态的SOAP消息呢?下面示例介绍怎样使用PyXml的DOM创建SOAP消息:
# post 一个 xml 文件, 使用 DOM 创建SOAP消息import sys, httplib from xml.dom import implementationfrom xml.dom.ext import PrettyPrint import StringIO # 定义所需的名字空间ec = "http://schemas.xmlsoap.org/soap/encoding/"soapEnv = "http://schemas.xmlsoap.org/soap/envelope/"myns = "http://phonedirlux.homeip.net/types" # DOM 文档domdoc = implementation.createDocument(None, '', None) # SOAP 封装名字空间seObj = domdoc.createElementNS(soapEnv, "SOAP-ENV:Envelope")seObj.setAttributeNS(soapEnv, "SOAP-ENV:encodingStyle", ec) # 加到根部domdoc.appendChild(seObj) header = domdoc.createElement("SOAP-ENV:Header")seObj.appendChild(header)body = domdoc.createElement("SOAP-ENV:Body")readls = domdoc.createElementNS(myns, "ns1:readLS")string_1 = domdoc.createElement("String_1")string_1.appendChild(domdoc.createTextNode("Message created with PyXml, your e-mail"))readls.appendChild(string_1)body.appendChild(readls) seObj.appendChild(body) soapStr = StringIO.StringIO()PrettyPrint(domdoc, soapStr) # 查看SOAP消息print soapStr.getvalue() # 构造头部并投递webservice = httplib.HTTP("www.pascalbotte.be")webservice.putrequest("POST", "/rcx-ws/rcx")webservice.putheader("Host", "www.pascalbotte.be")webservice.putheader("User-Agent", "My post")webservice.putheader("Content-type", "text/xml; charset=/"UTF-8/"")webservice.putheader("Content-length", "%d" % len(soapStr.getvalue()))webservice.putheader("SOAPAction", "/"/"")webservice.endheaders()webservice.send(soapStr.getvalue()) # 获取返回statuscode, statusmessage, header = webservice.getreply()print "Response: ", statuscode, statusmessageprint "headers: ", headerres = webservice.getfile().read()print res
转自:http://hi.baidu.com/moatlzy/blog/item/410fe43b5a12ebf83a87ce99.html