开始尝试一下soap,用python访问wsdl服务
2011-06-08 10:06
405 查看
实验一下天气预报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~~ ']
>>>
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~~ ']
>>>
相关文章推荐
- 失败尝试之------好的开始是成功的一半,借发哥一句广告词“我才刚刚上路”!没有任何事是一下就成功的!
- ASP.NET DEVELOPMENT SERVER 未能开始侦听端口xxxxx以一种访问权限不允许的方式做了一个访问套接字的尝试--错误处理
- python 分析nginx日志,并尝试一下stdin
- python 以一种访问权限不允许的方式做了一个访问套接字的尝试
- ASP.NET DEVELOPMENT SERVER 未能开始侦听端口xxxxx以一种访问权限不允许的方式做了一个访问套接字的尝试——解决方法
- [Python] 今天开始学习Python3了, 纪念一下
- python 抓一下 循环的访问也可以
- Python 访问soap服务
- ASP.NET DEVELOPMENT SERVER 未能开始侦听端口xxxxx以一种访问权限不允许的方式做了一个访问套接字的尝试.
- 应用AXIS开始Web服务之旅(soap web services)——使用三种不同的语言访问创建的Web服务,分别是JAVA、VB、VC
- 【发现】ASP.NET DEVELOPMENT SERVER 未能开始侦听端口xxxxx以一种访问权限不允许的方式做了一个访问套接字的尝试——解决方法
- Python解析器源码加密系列之(二):一次使用标准c的FILE*访问内存块的尝试
- 尝试向 URI“ ”发出请求时出错。这可能是由于试图以跨域方式访问服务而又没有正确的跨域策略,或策略不适用于 SOAP 服务...。出现此错误也可能是由于使用的是 Web 服务代理中的内部类型而没有使用 InternalsVisibleToAttribute 属性。
- 前天晚上写完年度总结,昨天修改了一下系统tag,今天起开始通读系统tag
- 尝试一下
- Python安装数据库SqlServer\MySql访问组件
- 开始自己的blog之旅,记录一下自己的工作中的一点感受
- virtualbox中宿主机如何访问linux虚拟机的python-flask网页
- python使用cStringIO实现临时内存文件访问的方法