从ActionScript调用Python

1 投票
1 回答
1574 浏览
提问于 2025-04-16 14:50

我有一个Adobe Air程序,它会调用一个Python脚本。我觉得ActionScript 3.0没有正确地进行调用。代码如下:

        var file:File;
        var args:Vector.<String> = new Vector.<String>;

            file = new File().resolvePath("/usr/bin/python");

            var pyScript:File;
            pyScript = File.applicationDirectory.resolvePath("python/mac/merge.py");

            var tempOutPath:String = File.applicationStorageDirectory.resolvePath("out.pdf").nativePath;
            args.push(pyScript.nativePath, "-w", "-o", tempOutPath, "-i");

            for(var x:int; x < numFilesToProcess; x++){

                var pdfPath:String = File(pdfs.getItemAt(x)).nativePath;

                args.push(pdfPath);

            }

            callNative(file, args);

在终端(Mac)中,下面的命令运行得很好:

python merge.py -w -o out.pdf -i file1.pdf file2.pdf

其中,args.push(pyScript.native.... 这一行是有问题的。我希望能得到一些帮助。

1 个回答

6

我遇到过类似的问题,使用的是Air。我的需求是从一个Air应用程序打印到收据打印机。但是我发现不能直接从应用程序里打印,所以我用Python写了一个RPC服务器来帮我完成这个任务,并通过http与它进行通信。下面是一个简化的版本,给你一个大概的了解:

这个Python RPC服务器的代码

from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/','/RPC2')

server = SimpleXMLRPCServer(('localhost', 8123), requestHandler=RequestHandler)

def myService( arg0, arg1 ):
    #do the stuff
    return 0

server.register_function(myService)
server.serve_forever()

在Air中,我把调用创建成一个XML字符串,然后发送请求。这里没有展示所有细节,因为我用的是JavaScript而不是ActionScript,所以请把这当作伪代码来看。

// XML as a string
// possibly create the XML and toXMLString() it?
var data:String = '
<?xml version="1.0"?>
<methodCall>
    <methodName>myService</methodName>
    <params>
        <param>
            <string>file1.pdf</string>
        </param>
        <param>
            <string>file2.pdf</string>
        </param>
    </params>
</methodCall>';

var req:URLRequest = new URLRequest('localhost:8123');
rec.method = 'POST';
rec.data = data;
var loader:URLLoader = new URLLoader( req );
//etc

撰写回答