zoukankan      html  css  js  c++  java
  • 最简实例,易上手,python 作为服务端,java 等语言进行调用

    整体思路是:

      python本身作为服务端和客户端,但是可以用其他语言,调用python命令,只用执行函数,然后获取函数返回值,那么其他语言就能获取python函数执行过的信息。

    python/server.py:

     1 import socket
     2 
     3 
     4 HOST = ''                 # Symbolic name meaning all available interfaces
     5 PORT = 50007              # Arbitrary non-privileged port
     6 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     7 s.bind((HOST, PORT))
     8 s.listen(1)
     9 while 1:
    10     conn, addr = s.accept()
    11     print('Connected by', addr)
    12     data = conn.recv(1024)
    13     if not data: break
    14     method=str(data)[2]
    15     if(method=="1"):
    16         conn.sendall(("111").encode())
    17     else:
    18         conn.sendall(("DDDDD").encode())
    19     # update plot
    20     conn.close()

    python/client.py

    import socket
    
    HOST = '127.0.0.1'  # The remote host
    PORT = 50007  # The same port as used by the server
    
    #调用服务端接口
    def cl_get_text():
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((HOST, PORT))
        s.sendall((str(1)).encode())
        data = []
        while True:
            subdata = s.recv(20480)
            if not subdata: break
            data.append(str(subdata, encoding='utf-8'))
        data= ''.join(data)
    
        s.close()
        return data
    
    
    if __name__ =="__main__":
        # 预测训练文件
        #将训练文件的预测结果以csv文件的形式存放到result中
        for i in range(10):
            print(i)
            print(cl_get_text())

    java:

     1 package com.util;
     2 import java.io.BufferedReader;
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 import java.io.InputStreamReader;
     6 import java.io.OutputStream;
     7 import java.io.UnsupportedEncodingException;
     8 import java.util.ArrayList;
     9 import java.util.List;
    10 
    11 
    12 public class Ppyrun {
    13     static final String workspace=Config.workspace;//client.py所在的文件夹
    14     public static void main(String args[])
    15     {
    16         String a=send_text();
    17         System.out.println(a);
    18     }
    19     
    20     
    21     public static String send_text()
    22     {
    23         String predict_result="";
    24         Runtime proc;
    25         proc = Runtime.getRuntime();
    26         try {
    27             Process result=proc.exec("python");
    28             OutputStream output=result.getOutputStream();
    29             runOutput(output,"import os");
    30             runOutput(output,"os.chdir('"+workspace+"')");
    31             runOutput(output,"import client");
    32             runOutput(output,"print(client.cl_get_text())");
    33             runFinish(output);
    34             InputStream input=result.getInputStream();
    35             BufferedReader br = null;
    36             br = new BufferedReader(new InputStreamReader(input, "GB2312"));
    37             String line = null;
    38             while ((line = br.readLine()) != null) {
    39                 predict_result+=line+"
    ";
    40             }
    41             input.close();
    42             result.waitFor();
    43         }catch (IOException | InterruptedException e) {
    44             // TODO 自动生成的 catch 块
    45             e.printStackTrace();
    46         }
    47         return predict_result;
    48     }
    49     
    50     
    51     public static void runOutput(OutputStream outstream,String command)
    52     {
    53         try {
    54 //            outstream.write((new String(command.getBytes("utf-8"),"GBK")+"
    ").getBytes("GBK"));
    55             outstream.write((command+"
    ").getBytes("UTF-8"));
    56         } catch (IOException e) {
    57             // TODO 自动生成的 catch 块
    58             e.printStackTrace();
    59         }
    60     }
    61 
    62 
    63     public static void runFinish(OutputStream outstream)
    64     {
    65         try {
    66             outstream.flush();
    67             outstream.close();
    68         } catch (IOException e) {
    69             // TODO 自动生成的 catch 块
    70             e.printStackTrace();
    71         }
    72         
    73     }
    74 }
  • 相关阅读:
    从零开始入门 K8s | 应用编排与管理
    209. Minimum Size Subarray Sum
    208. Implement Trie (Prefix Tree)
    207. Course Schedule
    203. Remove Linked List Elements
    183. Customers Who Never Order
    182. Duplicate Emails
    181. Employees Earning More Than Their Managers
    1261. Find Elements in a Contaminated Binary Tree
    1260. Shift 2D Grid
  • 原文地址:https://www.cnblogs.com/smartisn/p/14101291.html
Copyright © 2011-2022 走看看