本文章为原创,转载请注明出处!
登录平台:IOTOS®爱投斯物联中台
账号:iotos_test 密码:iotos123
代码地址:IOTOSDK-Python: IOTOS Python版本SDK,自带原生接口和采集引擎 (gitee.com)
目录
一、驱动目的
二、Web service杂谈
Web service三要素
三、驱动代码
四、驱动解析
五、使用示例
一、驱动目的
使用驱动开发Web services跨平台,跨语言,跨设备之间的通信。
二、Web service杂谈
Web service三要素
- SOAP:SOAP协议
- WSDL:Web service描述语言
- XML:可扩展性标记语言
三、驱动代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82#!coding:utf8 import json import time import traceback import logging import sys sys.path.append("..") from driver import * from soaplib import * from spyne import Application, rpc, ServiceBase from spyne import Integer, Unicode, Array from spyne.protocol.soap import Soap11 from spyne.server.wsgi import WsgiApplication from wsgiref.simple_server import make_server import sys from spyne.model.complex import ComplexModel from pymysql import connect import os,base64,logging logging.basicConfig(level=logging.DEBUG, filename='my_server.log') logging.getLogger('spyne.application.server').setLevel(logging.DEBUG) class Project(ComplexModel): __namespace__ = 'Project' name = Unicode phone = Unicode address = Unicode location = Unicode time = Unicode level = Unicode message = Unicode class SServices(ServiceBase): @rpc(Project, _returns=Unicode) def make_func(self, project): return u"链接成功,webservice 服务器已接收到数据" class AlcoholDriver(IOTOSDriverI): def InitComm(self,attrs): try: paramtmp = self.sysAttrs['config']['param'] ip = "127.0.0.1" port = 6667 soap_app = Application([SServices], 'SampleServices', in_protocol=Soap11(validator="lxml"), out_protocol=Soap11()) wsgi_app = WsgiApplication(soap_app) server = make_server(ip, port, wsgi_app) self.online(True) sys.exit(server.serve_forever()) except: traceback.print_exc() def Collecting(self, dataId): time.sleep(99999) return () def Event_customBroadcast(self, fromUuid, type, data): '''************************************************* TODO **************************************************''' return json.dumps({'code':0, 'msg':'', 'data':''}) def Event_getData(self, dataId, condition = ''): return json.dumps({'code':0, 'msg':'', 'data':''}) def Event_setData(self, dataId, value): return json.dumps({'code':0, 'msg':'', 'data':''}) def Event_syncPubMsg(self, point, value): '''************************************************* TODO **************************************************''' return json.dumps({'code':0, 'msg':'', 'data':''})
四、驱动解析
- 导入相应的包,运行环境为python2,且soaplib只支持python2、soaplib也不再更新了。
复制代码
1
2
3
4
5
6
7
8
9
10from soaplib import * from spyne import Application, rpc, ServiceBase from spyne import Integer, Unicode, Array from spyne.protocol.soap import Soap11 from spyne.server.wsgi import WsgiApplication from wsgiref.simple_server import make_server import sys from spyne.model.complex import ComplexModel from pymysql import connect import os,base64,logging
- 记录Web service服务端的logging文件。
复制代码
1
2logging.basicConfig(level=logging.DEBUG, filename='my_server.log') logging.getLogger('spyne.application.server').setLevel(logging.DEBUG)
- 声明接收的客户端的变量名、字段、xml标签。
数据多的话就用model,必须声明空间,创建对象或者字典都可以,即作为对象的一个属性,或者字典键值对来保存数据的传递。不然会报错!
复制代码
1
2
3
4
5
6
7
8
9class Project(ComplexModel): __namespace__ = 'Project' name = Unicode phone = Unicode address = Unicode location = Unicode time = Unicode level = Unicode message = Unicode
- 声明服务的类、方法,即客户端访问的服务、业务逻辑、操作。
复制代码
1
2
3
4class SServices(ServiceBase): @rpc(Project, _returns=Unicode) def make_func(self, project): return u"链接成功,webservice 服务器已接收到数据"
- 执行。ip、port是自己所选的地址
在驱动通讯初始化时创建SServices服务,服务调用make_fun函数。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16def InitComm(self,attrs): try: paramtmp = self.sysAttrs['config']['param'] ip = "127.0.0.1" port = 6667 soap_app = Application([SServices], 'SampleServices', in_protocol=Soap11(validator="lxml"), out_protocol=Soap11()) wsgi_app = WsgiApplication(soap_app) server = make_server(ip, port, wsgi_app) self.online(True) sys.exit(server.serve_forever()) except: traceback.print_exc()
- 设置驱动循环采集时长
复制代码
1
2
3def Collecting(self, dataId): time.sleep(99999) return ()
运行驱动后,在浏览器输入https//127.0.0.1:6667/SServices/?wsdl就能看到本地服务端已开启!
最后
以上就是伶俐大炮最近收集整理的关于IOTOS物联中台从0到1开发webservice接口驱动 实例详解的全部内容,更多相关IOTOS物联中台从0到1开发webservice接口驱动内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复