上海滩续集 gotv:基于soap协议使用python模拟短信网关

来源:百度文库 编辑:中财网 时间:2024/04/28 19:26:30

基于soap协议使用python模拟短信网关

    博客分类:
  • 应用
SOAPPythonSVNWebXML

问题1:soap协议是有什么作用?

答:soap提供了一种标准的方法,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信。

用一个简单的例子来说明soap使用过程,一个soap消息可以发送到一个具有Web Service功能的Web站点。

例如:一个含有房价信息的数据库,消息的参数中标明这是一个查询消息,此站点将返回一个XML格式的信息,其中包含了查询

结果(价格,位置,特点,或者其它信息)。由于数据是用一种标准化的可分析的结构来传递的,所以可以直接被第三方站点所

利用。

 

问题2:如何实现soap?

答:

简单理解,开放协议soap=rpc+http+xml,采用http作为底层通讯协议;rpc作为一致性的调用途径;xml作为数据传送的格式。

 

问题3:如何使用python实现基于soap协议的短信网关Web Service?

答:

Service

是使用soaplib搭建。

sudo apt-get install libxml2-dev

sudo apt-get install libxslt-dev

pip install soaplib

 

soaplib2.0.0.beta1

官网:http://soaplib.github.com/soaplib/2_0/

 

python实现soap协议的server.py

 

Python代码  
  1. import soaplib   
  2. from soaplib.core.service import rpc, DefinitionBase, soap   
  3. from soaplib.core.model.primitive import String, Integer   
  4. from soaplib.core.server import wsgi   
  5. from soaplib.core.model.clazz import Array   
  6.   
  7. useridDict = {"Jack":"123456"}   
  8.   
  9. def recordSM(tos, msg, time):   
  10.     _recordFile = open("../resource/sm"'a')   
  11.     _recordString = 'to:%s msg:%s time:%s\n' % (tos, msg, time)   
  12.     _recordFile.write(_recordString)   
  13.     _recordFile.close()   
  14.        
  15. class IsmgService(DefinitionBase):  
  16.     @soap(String, String, String, String, String, _returns=String)   
  17.     def sendSms(self, userid, password, mobiles, msg, time):   
  18.         if useridDict.has_key(userid):   
  19.             if useridDict[userid] == password:   
  20.                 recordSM(mobiles, msg, time)   
  21.                 return 'success'  
  22.         return 'lose'  
  23.   
  24. if __name__=='__main__':   
  25.     try:   
  26.         from wsgiref.simple_server import make_server   
  27.         soap_application = soaplib.core.Application([IsmgService], 'tns')   
  28.         wsgi_application = wsgi.Application(soap_application)   
  29.         server = make_server('localhost', 7789, wsgi_application)   
  30.         server.serve_forever()   
  31.     except ImportError:   
  32.         print "Error: example server code requires Python >= 2.5"  
 

 

Client

官网:https://fedorahosted.org/suds/

svn co http://svn.fedorahosted.org/svn/suds/trunk

pip install .

python实现的client

 

Python代码  
  1. from suds.client import Client   
  2. client = Client('http://localhost:7789/?wsdl')   
  3. userid = 'Jack'  
  4. password = '123456'  
  5. mobiles = '13812345678'  
  6. msg = 'hello world!'  
  7. time = '2011-03-01'  
  8.   
  9. result = client.service.sendSms(userid, password, mobiles, msg, time)   
  10.   
  11. print result  
 

 

备注:

出现错误 URLError: name or service not know

产生的原因 在不能与外网通信的情况下,soaplib和suds.client要去请求www.w3.org进行校验

解决方案:

把soaplib自动生成的wsdl保存到本地,去掉,用python -m SimpleHTTPServer起一个Http server

例如http://127.0.0.1:7788/wsdl,Client去引用这个URL,如Client('http://localhost:7788/wsdl')

 

问题4:什么是wsdl?

答:是基于XML的用于描述Web Services以及如何访问Web Services的语言。

这种文档可描述某个Web Services。它可规定服务的位置,以及此服务提供的操作(或方法)。

 

web service 执行的操作

它可描述一个web service, 可被执行的操作,以及相关的消息

可以把元素比作一个函数库

 

web service 使用的消息

每个消息均由一个或多个部件组成。可以把这些部件比作一个函数调用的参数

 

web service 使用的数据类型

为了最大程度的平台中立性,wsdl使用xml Schema语法来定义数据类型

 

web service 使用的通信协议

为每个端口定义消息格式和协议细节

binding元素有两个属性-name属性和type属性

name属性定义binding的名称,而type属性指向用于binding的端口。

 

具体作用:

1.服务做些什么--服务所提供的操作(方法)

2.如何访问服务--和服务交互的数据格式以及必要协议

3.服务位于何处--协议相关的地址,如URL

 

portType(与message和type元素的细节相结合)描述了Web服务是什么,binding元素描述了如何使用Web服务,port及service元素描述了Web服务的位置。

 

使用方式:

可以根据wsdl反向生成客户端代码,如python的suds.client库

是可以通过浏览器去访问的,但是需要wsdl文档支持。例如:URL是http://xxx/method?arg1=xx&arg2=xx

 

备注:

 

webService协议主要包括两个方面:传输协议和数据表示,关于传输协议可以是http或其它,数据表示也可以是键值对,xml或者其它。

 

问题5:什么是wsgi?

答:

wsgi的全写是Web Server Gateway interface。定义一个标准的沟通方式,让你写的程序可以

和服务器沟通,但是wsgi不是设计用来给任何语言使用的,它是设计给python用的。wsgi透过环

境变量来取得信息,例如request_method,server_name,http_xxx。

 

实例展示:

 

Python代码  
  1. from wsgiref.simple_server import make_server   
  2.   
  3. def my_app(environ, start_response):   
  4.     status = '200 OK'  
  5.     response_headers = [('Content-type''text/plain')]   
  6.     start_response(status, response_headers)   
  7.     return [u"你好! 歡迎來到Victor的第一個WSGI程式".encode('utf8')]   
  8.     
  9. httpd = make_server('', 8000, my_app)   
  10. print "Serving on port 8000..."  
  11. httpd.serve_forever()  

 

 wsgiref是python内建模块,通过浏览器访问http://127.0.0.1:8000

 

利用wsgi你可以写一个Middleware

 

Python代码  
  1. from wsgiref.simple_server import make_server   
  2.   
  3. def my_app(environ, start_response):   
  4.     status = '200 OK'  
  5.     response_headers = [('Content-type''text/plain')]   
  6.     start_response(status, response_headers)   
  7.     return ['hello world']   
  8.     
  9. class Middleware(object):   
  10.     def __init__(self, app, encoding='utf8'):   
  11.         self.app = app   
  12.         self.encoding = encoding   
  13.            
  14.     def __call__(self, environ, start_response):   
  15.         content = self.app(environ, start_response)   
  16.         for item in content:   
  17.             if item == 'hello world'return 'Hi!'  
  18.         return "hi!"  
  19.   
  20. app = Middleware(my_app)   
  21. httpd = make_server('', 8000, app)   
  22. print "Serving on port 8000..."  
  23. httpd.serve_forever()  

 

wsgi带来的好处:

最简单的网页程序做的事情就使用者发送一个request到你的服务器,接着你的程序产生内容,然后送出response回去给Browser。

复杂的事情如:session,cookie,认证,错误处理,而wsgi的Middleware将这些该做的事情限定于Middleware中,它们跟上下

游沟通都是按标准来实现,这些Middleware都能重复被利用。

 

详细描述整个请求过程:

首先,经过Registry Manager是用来管理request-local的相关物件。

接着,Status Code Redirect在Request上不做任何事情。

往下层跑,ErrorHandler也是不对Request做任何事。

接着,Cache Middle,视情况记录Request相关内容。

然后,是Session Middleware取出cookie还有在环境中准备session的资料。

接着,Routes依网址决定要给那个Controller来处理。

最后,Request经过层层关卡来到了真正的Pylons App,产生网页结果,Response会一层一层被回传回去。

到Cache有需要可以暂存下来,其中有例外抛出ErrorHandler会处理,Status Code Redirect可以重导页面到特定的错误页面