上文中webservice学习(1) ,使用soaplib建立了一个超简单的webservice服务,也是用suds调用成功了,那如果想使用http包自己组成一个soap消息来调用接口怎么办呢?
这个时候我们就想到使用wsdl这个文件了,我看了些wsdl的文档,也参照这其他人使用java,php等语言实现的soap消息调用的格式来写,但是怎么调试都没成功。。
就是说他总是会返回500或者是405各种错误,就是下面代码中的old_soap_body 变量中的消息格式。
#coding: utf-8
__author__= 'orangleliu'
__version__ = '0.1'
'''
filename: soaplib_test_http_client.py
create date: 2014-05-12
测试怎么样使用http来调用webservice,还没有解决,
不成功 总是返回找不到方法,很奇怪,也没有soap的文档。。。。。
'''
import httplib, urllib, urllib2, time
import pprint
import xml.dom.minidom as dm
soap_host = '127.0.0.1'
soap_port = 7789
old_soap_body = '''<?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:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body>
<tns:say_hello xmlns:tns="http://localhost:7789/">
<tns:name>orangleliu</tns:name>
<tns:times>10</tns:times>
</tns:say_hello>
</soap:Body>
</soap:Envelope>
'''
soap_body = '''<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="tns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
<ns1:say_hello>
<ns1:name>Dave</ns1:name>
<ns1:times>5</ns1:times>
</ns1:say_hello>
</ns0:Body>
</SOAP-ENV:Envelope>
'''
req_header = { 'Content-Type' : 'text/xml; charset=utf-8' }
conn = httplib.HTTPConnection(soap_host, soap_port, timeout=10)
conn.request('POST', '/', soap_body.encode('utf-8'), req_header)
response = conn.getresponse()
data = response.read()
conn.close()
#格式化打印xml文档
xml = dm.parseString(data)
print xml.toprettyxml()
后来这个soap_body是可以使用的,服务端启动之后,启动这个文件的到结果为
PS D:\CodeHouse\python\web\webservice> python .\soaplib_test_http_client.py
<?xml version="1.0" ?>
<senv:Envelope xmlns:plink="http://schemas.xmlsoap.org/ws/2003/05/partner-link/" xmlns:s12enc="http://www.w3.org/2003/05
/soap-encoding/" xmlns:s12env="http://www.w3.org/2003/05/soap-envelope/" xmlns:senc="http://schemas.xmlsoap.org/soap/enc
oding/" xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:
tns="tns" xmlns:wsa="http://schemas.xmlsoap.org/ws/2003/03/addressing" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xml
ns:xop="http://www.w3.org/2004/08/xop/include" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/
2001/XMLSchema-instance">
<senv:Body>
<tns:say_helloResponse>
<tns:say_helloResult>
<tns:string>Hello, Dave</tns:string>
<tns:string>Hello, Dave</tns:string>
<tns:string>Hello, Dave</tns:string>
<tns:string>Hello, Dave</tns:string>
<tns:string>Hello, Dave</tns:string>
</tns:say_helloResult>
</tns:say_helloResponse>
</senv:Body>
</senv:Envelope>
那么这个正确的soap格式是怎么找到的呢,还是使用上篇中说的suds客户端,只是把它发送到服务端的信息的log打印出来了,让后使用suds发送的消息格式来自己分装了下,然后调用就成功了。。
#coding: utf-8
__author__= 'orangleliu'
__version__ = '0.1'
'''
filename: soaplib_test_suds.py
create date: 2014-05-12
debug sudus访问soaplib服务端的内容 来看看怎么用soap xml写http请求
参考文章:https://mail.python.org/pipermail/soap/2011-July/000494.html
'''
import logging
from suds.client import Client
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
hello_client = Client('http://localhost:7789/?wsdl', cache=None)
result = hello_client.service.say_hello("Dave", 5)
print result
不过这也是一种解决问题的途径,因为restful api的兴起,webservice已经用的比原来少多了,但有时间还是要把wsdl格式研究下才好。