×

Web Service Python

python 开发webService

Kalet Kalet 发表于2019-05-10 18:15:46 浏览179 评论0

抢沙发发表评论

最近在学习用python 开发webservice,费了半天时间把环境搭好,记录下具体过程,以备后用。

首先系统上要有python。其次要用python进行webservice开发,还需要一些库:

1、lxml :
  命令行下 easy_install lxml 就能安装,如果不能正常安装下载lxml安装 :https://pypi.python.org/pypi/lxml/3.2.2,下载后,打开解压的文件夹,在当前文件夹中打开命令行工具,同时按中ctrl+shift键,点击“在此处打开命令行窗口” ,在命令行窗口输入python setup.py install 回车即可。

如下图:

  安装完成,如下图

  

 

 

2、pytz :
   命令行下  easy_install pytz 就能安装,如果不能正常安装下载pytz安装 :https://pypi.python.org/pypi/pytz/

3、zope:

  命令行下  easy_install zope 就能安装,如果不能正常安装下载zope安装 :https://pypi.python.org/pypi/zope.interface#download

4、pyOpenSSL

 命令行下  easy_install pyOpenSSL 就能安装,如果不能正常安装下载pyOpenSSL安装 :https://pypi.python.org/pypi/pyOpenSSL

5、twisted:

  命令行下  easy_install twisted 就能安装,如果不能正常安装下载twisted安装     https://pypi.python.org/pypi/Twisted/ or https://pypi.python.org/simple/twisted/

6、soaplib:

  命令行下  easy_install soaplib 就能安装,如果不能正常安装下载soaplib安装 :    https://github.com/soaplib/soaplib

7、suds:

  命令行下  easy_install suds 就能安装,如果不能正常安装下载suds安装 :    https://pypi.python.org/pypi/suds-jurko/0.6

由于上面的安装包存在依赖关系最好安装顺序下载和安装。安装完成后就可以进行webservice的开发和调用了。

直接贴代码:server.py

  

# coding: utf-8import soaplibfrom soaplib.core.util.wsgi_wrapper import run_twisted #发布服务from soaplib.core.server import wsgifrom soaplib.core.service import DefinitionBase  #所有服务类必须继承该类from soaplib.core.service import soap  #声明注解from soaplib.core.model.clazz import Array #声明要使用的类型from soaplib.core.model.clazz import ClassModel  #若服务返回类,该返回类必须是该类的子类from soaplib.core.model.primitive import Integer,Stringclass C_ProbeCdrModel(ClassModel):   __namespace__ = "C_ProbeCdrModel"   Name=String   Id=Integerclass HelloWorldService(DefinitionBase):  #this is a web service   @soap(String,_returns=String)    #声明一个服务,标识方法的参数以及返回值   def say_hello(self,name):      return 'hello %s!'%name   @soap(_returns=Array(String))   def GetCdrArray(self):      L_Result=["1","2","3"]      return L_Result   @soap(_returns=C_ProbeCdrModel)   def GetCdr(self):     #返回的是一个类,该类必须是ClassModel的子类,该类已经在上面定义      L_Model=C_ProbeCdrModel()      L_Model.Name=L_Model.Name      L_Model.Id=L_Model.Id      return L_Modelif __name__=='_main__':   soap_app=soaplib.core.Application([HelloWorldService], 'tns')   wsgi_app=wsgi.Application(soap_app)   print 'listening on 127.0.0.1:7789'   print 'wsdl is at: http://127.0.0.1:7789/SOAP/?wsdl'   run_twisted( ( (wsgi_app, "SOAP"),), 7789)if __name__=='__main__':  #发布服务   try:      from wsgiref.simple_server import make_server      soap_application = soaplib.core.Application([HelloWorldService], 'tns')      wsgi_application = wsgi.Application(soap_application)      server = make_server('localhost', 7789, wsgi_application)      server.serve_forever()   except ImportError:      print 'error'python server.py可以直接运行服务了。运行服务后打开浏览器,地址栏上键入: 就能看到描述服务的xml文档了。服务发布好后,就可以调用了,调用过程要用到suds库,上面已经提到。客户端调用代码:
# coding: utf-8from suds.client import Clienttest=Client('http://localhost:7789/SOAP/?wsdl')print test.service.say_hello(' world')这样就调用服务中say_hello这个服务方法了。

转载自:http://www.cnblogs.com/shaosks/p/6077037.html

群贤毕至

访客