开始尝试一下soap,用python访问
实验一下天气预报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~~ ']
>>>
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341