Skip to main content
 首页 » 编程设计

python之从 Actionscript 调用 python

2025年05月04日17lori

我有一个调用 python 脚本的 Adob​​e Air 程序。我认为 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.... 行是有问题的行。我会很感激一些帮助。

请您参考如下方法:

我在使用 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