0

I'm trying to run python script from java and when something would change in java I want to send information about it to python program. I don't know the best solution for it. I can run python script and send start information to it but then problems start. I think about sending data through tcp/ip connection, but when I try to do that I have error in python script:

Caused by: Traceback (most recent call last):
File "pythonScript.py", line 2, in <module>
import socket
ImportError: No module named socket
at org.python.core.Py.ImportError(Py.java:264)
at org.python.core.imp.import_first(imp.java:657)
at org.python.core.imp.import_name(imp.java:741)
at org.python.core.imp.importName(imp.java:791)
at org.python.core.ImportFunction.__call__(__builtin__.java:1236)
at org.python.core.PyObject.__call__(PyObject.java:367)
at org.python.core.__builtin__.__import__(__builtin__.java:1207)
at org.python.core.__builtin__.__import__(__builtin__.java:1190)
at org.python.core.imp.importOne(imp.java:802)
at org.python.pycode._pyx0.f0ドル(pythonScript.py:27)
at org.python.pycode._pyx0.call_function(pythonScript.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1197)
at org.python.core.__builtin__.execfile_flags(__builtin__.java:538)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:156)
at sample.PythonClass.runPythonScript(PythonClass.java:26)
at sample.Controller.handleSubmitButtonActionIp(Controller.java:30)
... 58 more

So it's some problem with scoket import, but when I run this program normaly there is no error. It's code of function which I use to run python script:

 public void runPythonScript(boolean isCameraOn, String ip){
 System.out.println(ip);
 String[] arguments = {ip};
 PythonInterpreter.initialize(System.getProperties(),System.getProperties(), arguments);
 PythonInterpreter python = new PythonInterpreter();
 StringWriter out = new StringWriter();
 python.setOut(out);
 python.execfile("pythonScript.py");
 String outputStr = out.toString();
 System.out.println(outputStr);
} 

And it's code of python client:

import sys
import socket //ERROR
print("poczatek")
print(sys.argv[0])
print("koniec")
HOST = "localhost"
PORT = 8080
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.connect((HOST, PORT))
sock.sendall("Hello\n")
data = sock.recv(1024)
print("1)", data)
if (data == "olleH\n"):
 sock.sendall("Bye\n")
 data = sock.recv(1024)
 print("2)", data)
if (data == "eyB}\n"):
 sock.close()
 print("Socket closed")

Java server:

 public void sendDataToPythonScript(boolean isCameraOn, String ip) throws 
 IOException {
 String fromClient;
 String toClient;
 ServerSocket server = new ServerSocket(8080);
 System.out.println("wait for connection on port 8080");
 boolean run = true;
 while(run) {
 Socket client = server.accept();
 System.out.println("got connection on port 8080");
 BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
 PrintWriter out = new PrintWriter(client.getOutputStream(),true);
 fromClient = in.readLine();
 System.out.println("received: " + fromClient);
 if(fromClient.equals("Hello")) {
 toClient = "olleH";
 System.out.println("send olleH");
 out.println(toClient);
 fromClient = in.readLine();
 System.out.println("received: " + fromClient);
 if(fromClient.equals("Bye")) {
 toClient = "eyB";
 System.out.println("send eyB");
 out.println(toClient);
 client.close();
 run = false;
 System.out.println("socket closed");
 }
 }
 }
 System.exit(0);
}
asked Aug 15, 2017 at 19:54
0

1 Answer 1

1

Try importing the embedded socket module

import _socket

If that does not fix it try setting the path for python modules this link should explain how to set the path for python in java.

answered Aug 15, 2017 at 20:12
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I set python path and now It's working. I will edit post later with working code.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.