您的位置:首页 > 其它

Web Service系列之实例之使用urllib发送SOAP POST请求

2017-04-21 21:49 766 查看
原文链接: Web Service系列之实例之使用urllib发送SOAP POST请求

完整代码:

import urllib.request

url_jaxws = "http://localhost:9000/WS/HelloWorld"
url_spyne = "http://localhost:8000/"

data_jaxws = '''<?xml version='1.0' encoding='utf-8'?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Body>
<ns0:helloWorld xmlns:ns0="http://webservice.kylinux.com/">
<arg0>Kylin</arg0>
<arg0>Shu</arg0>
</ns0:helloWorld>
</soap-env:Body>
</soap-env:Envelope>
'''

data_spyne = '''<?xml version='1.0' encoding='utf-8'?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Body>
<ns0:say_hello_plus2 xmlns:ns0="spyne.examples.hello.soap">
<ns0:name_plus>
<ns0:string>Kylin</ns0:string>
<ns0:string>Shu</ns0:string>
</ns0:name_plus>
</ns0:say_hello_plus2>
</soap-env:Body>
</soap-env:Envelope>
'''

headers = {'Content-Type': 'text/xml'}

req = urllib.request.Request(url_jaxws, data=data_jaxws.encode('utf-8'), headers=headers)
res = urllib.request.urlopen(req)
print(res.read().decode('utf-8'))

req = urllib.request.Request(url_spyne, data=data_spyne.encode('utf-8'), headers=headers)
res = urllib.request.urlopen(req)
print(res.read().decode('utf-8'))


输出结果:

➜  websvr python3 ./invoke_ws.py
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:helloWorldResponse xmlns:ns2="http://webservice.kylinux.com/"><return>Hello world from KylinShu</return></ns2:helloWorldResponse></S:Body></S:Envelope>
<?xml version='1.0' encoding='UTF-8'?>
<soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="spyne.examples.hello.soap"><soap11env:Body><tns:say_hello_plus2Response><tns:say_hello_plus2Result><tns:string>Kylin</tns:string><tns:string>Shu</tns:string></tns:say_hello_plus2Result></tns:say_hello_plus2Response></soap11env:Body></soap11env:Envelope>


问题:

那么,
data_jaxws
data_spyne
中的名字空间(
<ns0:say_hello_plus2 xmlns:ns0="spyne.examples.hello.soap">
中的
spyne.examples.hello.soap
)的值从哪里取呢?

参数名(
<ns0:string>Kylin</ns0:string>
中的
string
)的值又如何确定呢?

名字空间

名字空间的值取自wsdl(如
http://localhost:8000/?wsdl
)的
<definitions>
<wsdl:definitions>
节点中
xmlns:tns
targetNamespace
的值

如,

xmlns:tns="spyne.examples.hello.soap"
targetNamespace="spyne.examples.hello.soap"


参数

参数名取自wsdl的
<wsdl:types>
节点

如,

<wsdl:types>
<xs:schema targetNamespace="spyne.examples.hello.soap" elementFormDefault="qualified">
<xs:complexType name="stringArray">
<xs:sequence>
<xs:element name="string" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
</xs:sequence>
</xs:complexType>
... ...
</xs:schema>
</wsdl:types>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: