如何从wso2代理服务调用python脚本文件

时间:2019-04-07 06:47:32

标签: wso2 wso2esb wso2ei

如何从wso2代理服务调用python脚本文件。

我们尝试与send mediator一起调用位于本地计算机中的python脚本文件。

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="FilepythonTest"
       transports="http https"
       startOnLoad="true">
   <description/>
   <target >
      <inSequence>
         <send>
            <endpoint>
               <address uri="local:///Users/vikashsaharan/Desktop/python/testpy.py"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <log level="full"/>
      </outSequence>
   </target>
</proxy>

我们无法通过此电话致电。请指导我如何从wso2调用python脚本

2 个答案:

答案 0 :(得分:6)

WSO2 EI具有内置功能,可以使用Script mediator执行python脚本。以下是一个示例配置。

**stockquoteTransformResponse.py file saved in carbon registry.**

from org.apache.synapse.util.xpath import SynapseXPath

def transformRequest(mc):
    symbolXPath = SynapseXPath("//*[local-name()='Code']/text()")
    symbol = symbolXPath.stringValueOf(mc)
    mc.setPayloadXML('''
	<m:getQuote xmlns:m="http://services.samples">
		<m:request>
			<m:symbol>''' + symbol + '''</m:symbol>
		</m:request>
	</m:getQuote>''')

C=@(location,state)abs(cos(location.y/2)+sin(location.x/2));
F=C;
specifyCoefficients(model,'m',0,...
                          'd',0,...
                          'c',C,...
                          'a',0,...
                          'f',F);

我们需要将jython jar添加到 WSO2EI_HOME / lib 目录。已使用来自http://central.maven.org/maven2/org/python/jython/2.2.1/jython-2.2.1.jar

的jython-2.2.1.jar进行了测试

一旦调用上述api,便可以看到以下输出。 enter image description here

答案 1 :(得分:1)

您可以使用class mediator并从那里执行python脚本。以下是可以执行此操作的示例类介体。

public boolean mediate(MessageContext context) { 
        String command = "python /path/to/script.py";
        try {
            Process p = Runtime.getRuntime().exec(command);
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String ret = in.readLine();
            System.out.println("value is : "+ret);
        } catch (IOException e) {
            // handle exception
        }
        return true;
    }

您可以参考Running a .py file from Java