开始尝试一下soap,用python访问wsdl服务
来源:互联网 发布:boker plus淘宝 编辑:程序博客网 时间:2024/05/21 04:26
实验一下天气预报Webservice服务,数据来源于中国气象局:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
python的程序:如果需要库支持,下载地址:http://pypi.python.org/pypi?%3Aaction=index
推荐使用:setuptools,安装后可以使用easy_install很方便
安装fpconst:easy_install.py fpconst
SOAPpy 下载地址:http://pywebsvcs.sourceforge.net/
>>>from SOAPpy import WSDL
>>>wsdlFile = ‘http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl’
>>>server = WSDL.Proxy(wsdlFile)
>>>server.methods
--------------------------- V
{u'getWeatherbyCityNamePro': <SOAPpy.wstools.WSDLTools.SOAPCallInfo instance at 0x01168A58>, u'getSu
pportCity': <SOAPpy.wstools.WSDLTools.SOAPCallInfo instance at 0x011686E8>, u'getWeatherbyCityName':
<SOAPpy.wstools.WSDLTools.SOAPCallInfo instance at 0x01168940>, u'getSupportDataSet': <SOAPpy.wstoo
ls.WSDLTools.SOAPCallInfo instance at 0x01168828>, u'getSupportProvince': <SOAPpy.wstools.WSDLTools.
SOAPCallInfo instance at 0x01168760>}
---------------------------- A
>>> for a in server.getSupportProvince():
... for i in a:
... print i
------------------------------ V
直辖市
特别行政区
黑龙江
吉林
辽宁
内蒙古
河北
河南
山东
山西
江苏
安徽
陕西
宁夏
甘肃
青海
湖北
湖南
浙江
江西
福建
贵州
四川
广东
广西
云南
海南
新疆
西藏
台湾
亚洲
欧洲
非洲
北美洲
南美洲
大洋洲
---------------------------------------------- 用django搞个简单的soap服务:
>>>easy_install.py soaplib
>>>easy_install.py django
>>>django-admin.py startproject mysite
soaplib_handler.py
from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers import primitive as soap_types
import StringIO
from django.http import HttpResponse
class DumbStringIO(StringIO.StringIO):
def read(self, n):
return self.getvalue()
class DjangoSoapApp(SimpleWSGISoapApp):
def __call__(self, request):
django_response = HttpResponse()
def start_response(status, headers):
status, reason = status.split(' ', 1)
django_response.status_code = int(status)
for header, value in headers:
django_response[header] = value
environ = request.META.copy()
body = ''.join(['%s=%s' % v for v in request.POST.items()])
environ['CONTENT_LENGTH'] = len(body)
environ['wsgi.input'] = DumbStringIO(body)
environ['wsgi.multithread'] = False
response = super(DjangoSoapApp, self).__call__(environ, start_response)
django_response.content = "/n".join(response)
return django_response
views.py
from django.http import HttpResponse
from soaplib_handler import DjangoSoapApp, soapmethod, soap_types
def hello(request):
return HttpResponse("Hello world")
class HelloWorldService(DjangoSoapApp):
__tns__ = 'http://localhost:8000/soap/'
@soapmethod(_returns=soap_types.Array(soap_types.String))
def say_hello(self):
results = []
results.append('Hello, Here is the first webservice test~~ ')
return results
hello_world_service = HelloWorldService()
urls.py --------------------
from django.conf.urls.defaults import *
from views import hello
from views import hello_world_service
urlpatterns = patterns('',
('^$', hello),
(r'^hello_world/', hello_world_service),
(r'^hello_world/service.wsdl', hello_world_service),
)
http://github.com/jkp/soaplib/issues/#issue/2 unicode错误补丁。
测试自己的soap服务:
>>> from SOAPpy import WSDL
>>> wsdl='http://127.0.0.1:8000/hello_world/service.wsdl'
>>> server = WSDL.Proxy(wsdl)
>>> server.methods
{u'say_hello': <SOAPpy.wstools.WSDLTools.SOAPCallInfo instance at 0x00E155F8>}
-----------------------A 竟然报错。。。贴一下别人提供的方法,但是实验了还是有问题。
OK, so mi fixes are simple.
Open serializers/primitive.py
and change line 445 to/
"%s:%s" % (self.serializer.get_namespace_id(), self.serializer.get_datatype=()))
and
soaplib/soap.py line 118 to
root, xmlids = ElementTree.XMLID(xml_string.encode()) and this makes by services and WSDL files working both in SoapUI and standard PHP SOAP without any problems
参考一下:http://hi.baidu.com/derris/blog/item/f68ad0de4c01a45a95ee371c.html
另一种本地测试没有问题:
>>> from soaplib.client import make_service_client
>>> from views import HelloWorldService
>>> client = make_service_client('http://127.0.0.1:8000/hello_world/',HelloWorldService())
>>> print client.say_hello()
['Hello, Here is the first webservice test~~ ']
>>>
- 开始尝试一下soap,用python访问wsdl服务
- 用soap调用webservice的WSDL服务
- Studying python/SOAP/WSDL
- 使用 WSDL 部署 Web 服务,第 2 部分: 简单对象访问协议(SOAP)
- [转]使用 WSDL 部署 Web 服务,第 2 部分: 简单对象访问协议(SOAP)
- 使用 WSDL 部署 Web 服务,第 2 部分: 简单对象访问协议(SOAP)
- 使用 WSDL 部署 Web 服务,第 2 部分: 简单对象访问协议(SOAP)
- SOAP访问Web服务
- web服务中soap、wsdl、uddi理解
- PHP 生成WSDL 以及 提供SOAP服务
- python cgi服务尝试
- 初步理解一下:SOA, SOAP, Web Service, WSDL等
- wsdl & soap
- Soap wsdl
- 稍微开始尝试写一下博客WWWW
- 使用SOAP访问Web服务
- 使用SOAP访问Web服务
- 使用SOAP访问Web服务
- Sql 根据子节点查找根节点
- GridView 选择变色
- 地图随意搜---看到最近人人的工作
- C++模板使用介绍
- asp.net页面事件执行顺序
- 开始尝试一下soap,用python访问wsdl服务
- Linux undefined reference to `cos'
- BSEG与BSIS、BSAS、BSID、BSAD、BSIK、BSAK之间的关系
- 复杂文件到别一个文件夹中
- Lou's Pseudo 3d Page v0.91
- 分享给想学python的朋友
- oracle集中备份
- Linux下挂载移动硬盘和USB的方法
- 谷歌Android开发常见错误及技巧