Calling python from Actionscript
I have an Adobe Air Program that calls a python script. I dont' think the actionscript 3.0 is making the proper call. Code:
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);
In terminal (Mac), the following works fine:
python merge.py -w -o out.pdf -i file1.pdf file2.pdf
The args.push(pyScript.native.... line is the problematic one. I'd appreciate so开发者_如何转开发me assistance.
I have faced a similar problem using Air. I needed to print to a receipt printer from an Air app. I couldn't do it from the app itself so I used a python RPC server to do the work for me and talked to it over http. below is a simplified version to give you an idea:
The python RPC server
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()
In Air I create the call as an XML string and make my request. I haven't shown all the details as I was using javascript not actionscript so please treat this as pseudocode.
// 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
精彩评论