发帖

【知淘出品】基于Python的WebService与氚云系统集成

产品使用讨论区  / 应用搭建教程  / 倒序浏览   © 著作权归作者本人所有

#楼主# 2020-3-14

跳转到指定楼层

注册氚云社区,学习低代码知识,与更多氚友互动交流

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 北京知淘科技有限责任公司 于 2021-2-26 16:43 编辑

最近一直在用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;
}
                                                                                                                               魏晓峰                                                                                                                               氚云定制,+微信okkeman
                                                                                                                               2020年3月14日








回复

使用道具

60

主题

198

帖子

131万

金币

管理员

Rank: 127Rank: 127Rank: 127Rank: 127Rank: 127Rank: 127Rank: 127

积分
7807

奥哲周年勋章2021跨年勋章内测荣誉勋章氚云标准版氚云专业版

社区小站长 发表于 2020-3-14 22:16:51
感谢您发布原创的优质内容,小编特打赏200金币!   
回复

使用道具 举报

1

主题

4

帖子

91

金币

lv3

Rank: 3Rank: 3Rank: 3

积分
91

氚云专业版氚云标准版

屏风山人 发表于 2020-3-27 19:45:36
感谢楼主提供如此详细的分享!
回复

使用道具 举报

0

主题

5

帖子

86

金币

lv3

Rank: 3Rank: 3Rank: 3

积分
116
zjujunge 发表于 2021-2-21 20:11:34
请问氚云后台调用代码时机是在onLoad中调用吗
回复

使用道具 举报

北京知淘科技有限责任公司 发表于 2021-2-22 09:39:04
zjujunge 发表于 2021-2-21 20:11
请问氚云后台调用代码时机是在onLoad中调用吗

根据你自己需求来写:
Onload是打开页面时执行;
OnSubmit是提交数据的时候执行
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|氚云社区 |粤ICP备15096637号-1
Powered by Discuz! X3.4 Licensed  © 2001-2017 Comsenz Inc.