0
我想從java運行python腳本,當在java中的東西會改變我想發送有關它的信息到python程序。我不知道它的最佳解決方案。我可以運行python腳本並將啓動信息發送給它,但隨後出現問題。我想通過TCP/IP連接發送的數據,但是當我嘗試這樣做,我有python腳本錯誤:最新的通信java到python
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.f$0(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
所以它的一些問題SCOKET進口,但是當我運行這個程序normaly不存在錯誤。它的功能代碼,我用它來運行python腳本:
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);
}
而且它的Python客戶端的代碼:
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服務器:
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);
}
謝謝,我設置了python路徑,現在它工作。稍後我將使用工作代碼編輯帖子。 – Marcin46