irmen / Pyrolite

Java and .NET client interface for Pyro5 protocol
MIT License
177 stars 47 forks source link

Run python script using Pyrolite #17

Closed lasidaputhunattil closed 9 years ago

lasidaputhunattil commented 9 years ago

Hi,

I have a simple python script in my local machine, which returns a string. I want to run this script from java application and get the return value. I'm trying to do this using Pyrolite. I downloaded the jar files and added them to my java class path. But I'm not able to run the script.

I got the below sample code from readme.txt

NameServerProxy ns = NameServerProxy.locateNS(null); PyroProxy remoteobject = new PyroProxy(ns.lookup("Your.Pyro.Object")); Object result = remoteobject.call("pythonmethod", 42, "hello", new int[]{1,2,3}); String message = (String)result; // cast to the type that 'pythonmethod' returns System.out.println("result message="+message); remoteobject.close(); ns.close();

But this is not working for me. My system configuration is

OS: Windows 8 JDK: jdk1.7.0_51 Python: 2.6

Please help me with this.

irmen commented 9 years ago

You're not very helpful. It's impossible to assist if you don't provide extra info such as:

p.s. while Pyro still supports it, Python 2.6 is very old. Any particular reason you're still using this old version instead of 2.7 or 3.x?

p.p.s. you write : 'script on my local machine'. Is it just a single python script that you want to execute from within a Java program? Then Pyrolite is perhaps not the right choice, it is used to connect your Java program to a running python server process (possibly on a different machine) using the Pyro protocol.

lasidaputhunattil commented 9 years ago

Hi,

Sorry for providing insufficient information. This is what I tried till now:

    NameServerProxy ns = NameServerProxy.locateNS(null);
    PyroProxy remoteobject = new PyroProxy();
    Object result = remoteobject.call("C:\\trail.py", null);
    String message = (String)result; 
    System.out.println("result message="+message);
    remoteobject.close();
    ns.close();

but it is giving be following error: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.(Unknown Source) at java.net.Socket.(Unknown Source) at net.razorvine.pyro.PyroProxy.connect(PyroProxy.java:72) at net.razorvine.pyro.PyroProxy.internal_call(PyroProxy.java:196) at net.razorvine.pyro.PyroProxy.call(PyroProxy.java:161) at tryPyrolite.pyrotry.main(pyrotry.java:15)

Since the script is in my own local machine I thought this is enough, but something I'm missing out. Since our whole backend still work on Python 2.6, I cant upgrade my python.

Also my sample python script is this: import ctypes p=ctypes.create_string_buffer('Hello') print p.value

Please tell me if you need more information to help me out with this. I'm stuck with this from past 1 week.

irmen commented 9 years ago

Seems like you're confused as to what you want to do: you're using the wrong tool for the job.

Pyrolite is not for executing arbitrary python programs on the local machine. It is meant to connect over the network to a long running python process via the Pyro protocol.

My guess is you wanna use Runtime.exec() or ProcessBuilder instead.