最新的comunnication java to python

时间:2017-08-15 19:54:06

标签: java python jython

我正在尝试从java运行python脚本,当java中的某些内容发生变化时,我想将有关它的信息发送到python程序。我不知道最好的解决方案。我可以运行python脚本并向其发送启动信息,但问题就开始了。我想通过tcp / ip连接发送数据,但是当我尝试这样做时,我在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导入存在一些问题,但是当我正常运行此程序时,没有错误。这是我用来运行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);
}

1 个答案:

答案 0 :(得分:1)

尝试导入嵌入式套接字模块

import _socket

如果没有解决它,请尝试设置python模块的路径this link应该解释如何在java中设置python的路径。