最近一直在用Python做web项目,有些数据需要由webservice传输给氚云应用,但氚云的帮助文档里只有C#集成案例。经过一番摸索,终于调通了,这里把几点经验分享下。
1. Python提供的webservice接口地址以“?wsdl”结尾,但在氚云系统集成中,需要将该结尾删除才能连接成功
2. Python中的webservice接口传出协议应该用soap11,不要用soap12
3.Invoke函数必须按照氚云要求的json字符串格式返回,否则氚云无法正确调用数据
4.由于我没有租服务器,采用的是花生壳内网映射的办法,所以host写的是‘127.0.0.1’,你如果租的有服务器的话,可以直接写服务器的IP地址
5.这里主要分享的是氚云调用Python数据,如果用Python调用氚云,直接调用氚云提供的openAPI接口即可,比较简单。可参考我的另外一篇帖子:《Python端调用氚云数据案例》
6.python需要安装spyne库
-------------------------------以下是Python的webservice服务端代码------------------------------
from spyne import Application, rpc, ServiceBase
from spyne import Integer, Unicode, Array, ComplexModel, Iterable, String
from spyne.protocol.soap import Soap11, Soap12
from spyne.server.wsgi import WsgiApplication
from wsgiref.simple__server import make_server
import json
# 数据指标class ItemSchema:
def __init__(self, name, DisplayName, DataType):
self.name = name
self.DisplayName = DisplayName
self.DataType = DataType
# 数据模板class StructureSchema:
__SchemaList = []
__Code = ''
def __init__(self, Code):
StructureSchema.__SchemaList.clear()
StructureSchema.__Code = Code
def Add(self, ItemSchemaData):
StructureSchema.__SchemaList.append(ItemSchemaData)
def JsonToSchema(self):
Result = {"Code": StructureSchema.__Code}
Items = []
for obj in StructureSchema.__SchemaList:
strJson = {"Name": obj.name, "DisplayName": obj.DisplayName, "DataType": obj.DataType}
Items.append(strJson)
Result["Items"] = Items
return Result
# 数据内容class StructureData:
def __init__(self, name, DisplayName):
self.name = name
self.DisplayName = DisplayName
class ReturMessage:
def __init__(self, ResultCode, ErrorMessage):
self.ResultCode = ResultCode
self.ErrorMessage = ErrorMessage
# 生成氚云格式的json字符串
def GernerateResult(schemaCode):
itemName = ItemSchema("Name", "学生姓名", "String")
itemAge = ItemSchema("Age", "年龄", "Int")
SchemaTemplate = StructureSchema(schemaCode)
SchemaTemplate.Add(itemName)
SchemaTemplate.Add(itemAge)
JsonSchemaTemplate = SchemaTemplate.JsonToSchema()
results = {
"ResultCode": 0,
"Message": "",
"Schema": JsonSchemaTemplate,
"Data": {"Name": "张三", "Age": 10}
}
return json.dumps(results)
class H3yunWebService(ServiceBase):
@rpc(Unicode, _returns=Unicode)
def GetSchema(self, schemaCode):
itemName = ItemSchema("Name", "学生姓名", "String")
itemAge = ItemSchema("Age", "年龄", "Int")
SchemaTemplate = StructureSchema(schemaCode)
SchemaTemplate.Add(itemName)
SchemaTemplate.Add(itemAge)
JsonSchemaTemplate = SchemaTemplate.JsonToSchema()
return json.dumps(JsonSchemaTemplate)
@rpc(_returns=Unicode)
def GetSchemaList(self):
schemaList = {"XXX":"学生信息"}
return json.dumps(schemaList)
@rpc(Unicode, Unicode, Unicode, _returns=Unicode)
def GetList(self, userCode, schemaCode, filter):
results = {}
return json.dumps(results)
@rpc(Unicode, Unicode, Unicode, Unicode, _returns=Unicode)
def Invoke(self, userCode, schemaCode, methodName, param):
results = GernerateResult("XXX")
return results
application = Application([H3yunWebService],
'http://tempuri.org/',
in_protocol=Soap11(validator='lxml'),
out_protocol=Soap11())
wsgi_application = WsgiApplication(application)
if __name__ == '__main__':
import logging
host = '127.0.0.1'
port = 8000
logging.info("listening to http://127.0.0.1:8000")
logging.info("wsdl is at: http://localhost:8000/?wsdl")
server = make_server(host, port, wsgi_application)
server.serve_forever()
-------------------------以下是氚云后台调用代码-------------------
H3.BizBus.BizStructureSchema paramSchema1 = new H3.BizBus.BizStructureSchema();
paramSchema1.Add(new H3.BizBus.ItemSchema("Name", "学生姓名", H3.Data.BizDataType.ShortString, 200, null));
paramSchema1.Add(new H3.BizBus.ItemSchema("Age", "年龄", H3.Data.BizDataType.Int, 200, null));
H3.BizBus.BizStructure paramData1 = new H3.BizBus.BizStructure(paramSchema1);
paramData1["Name"] = "李四";
paramData1["Age"] = 25;
H3.BizBus.InvokeResult inResult1 = this.Request.Engine.BizBus.Invoke(H3.Organization.User.SystemUserId, H3.BizBus.AccessPointType.Legacy, "XXX", "many", paramData1);
if(inResult1 != null && inResult1.Code == 0)
{
H3.BizBus.BizStructure returnData = inResult1.Data;
string data = returnData["Name"] as string;
}