一.需求背景
算法团队使用python作为开发语言,web系统的开发人员使用java,web系统提供了一些页面操作,用户点击
按钮之后,java调用python脚本进行处理。
二.错误的选择
这个项目之前,笔者并没有过java系统调用python的经验,并且项目时间紧张,使用了最直接的调用方式,就是本地环境调用java脚本。代码如下:
public static boolean invokePython(String[] args){
try {
String line = null;
Process proc = Runtime.getRuntime().exec(args);// 执行py文件
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "utf-8")); //gbk 避免汉字乱码
List<String> list = Lists.newArrayList();
while ((line = in.readLine()) != null) {
//logger.info(line);
list.add(line);
}
in.close();
int exitVal=proc.waitFor();
logger.info("res:"+exitVal);
/**Process exitValue: 0 调用python脚本成功
Process exitValue: 1 java调用python失败
Process exitValue: 2 python脚本执行失败**/
if(exitVal==0){
return true;
}
if(exitVal==2){
logger.warn("java调用python失败",JSON.toJSON(args));
return false;
}
else{
logger.warn("python脚本执行失败",JSON.toJSON(args));
return false;
}
} catch (IOException e) {
logger.warn("invokePython_fail_param_{}",JSON.toJSON(args),e);
return false;
} catch (InterruptedException e) {
logger.warn("invokePython_fail_param_{}",JSON.toJSON(args),e);
System.out.println(e.getStackTrace());
return false;
}
}
这种方式有很多弊端,
1.最明显的就是python脚本单独运行并没有问题,但是java调用时就报错了,主要是项目路径,项目字符集的问题
2.本地开发环境配置复杂,python脚本只能放在本地,所以java开发人员还要配置python的环境,大部分python第三方包还是很好装,有的像tensorflow这种就会有不少问题,windows环境下,python3.5支持tensorflow,其他的3.6,3.7目前并不支持
3.调试起来麻烦,java调用脚本,脚本一旦报错的话,由于不能打断点,就要在加一些print和try catch去判断哪里出问题,这种方式对于简单代码还可以,复杂的话就要花不少时间。
由于以上几个弊端,笔者对此种调用方式深恶痛绝
三.http接口调用的方式
摆在面前的出路是python提供http服务,这就需要我们搭建一个python的框架,这里我使用了Django,Django框架搭建起来
并不复杂,具体做法可以自行百度。
搭建好Django之后,需要增加java发送http请求的工具类,代码如下:
public static String httpGetRequest(String url) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
// HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
// 创建UrlEncodedFormEntity对象
HttpResponse response = httpClient.execute(httpGet);
String html = extractContent(response);
httpClient.close();
return html;
}
public static String httpPostRequest(String url, Map<String, String> params) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
try{
// HttpClient httpclient = new DefaultHttpClient();
/** liupeng加入 读取超时时间=3分钟,默认=1分钟 */
// httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3*60*1000);
// httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3*60*1000);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3*60*1000).setConnectTimeout(3*60*1000).build();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
httpPost.setConfig(requestConfig);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
for (String key : params.keySet()) {
parameters.add(new BasicNameValuePair(key, params.get(key)));
}
// 创建UrlEncodedFormEntity对象
UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(parameters, "UTF-8");
httpPost.setEntity(formEntiry);
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
logger.info("httpPostRequest11: " + response.toString());
}else{
logger.info("httpPostRequest22 ");
}
String html = extractContent(response);
return html;
}catch(Exception e){
logger.error(" httpPostRequest ",e);
throw new RuntimeException(e);
} finally {
if (null != httpClient) {
httpClient.close();
}
}
}
private static String extractContent(HttpResponse response) throws Exception {
String htmStr = null;
logger.info(" extractContentgetStatusCode " + response.getStatusLine().getStatusCode());
logger.info(" content " + response.getEntity().getContent().toString());
if (response.getStatusLine().getStatusCode() == 200) {
if (response != null) {
HttpEntity entity = response.getEntity();
InputStream ins = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
sbf.append(line);
}
br.close();
// 处理内容
htmStr = sbf.toString();
}
}else{
if (response != null) {
HttpEntity entity = response.getEntity();
InputStream ins = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
sbf.append(line);
}
br.close();
// 处理内容
htmStr = sbf.toString();
logger.info(" 处理内容:" + htmStr);
}
return ""; //失败时记录日志,但是结果返回空值
}
return htmStr;
}
调用时发现报了Forbidden (CSRF cookie not set.): 错误:
临时解决方案:修改settings.py文件,注释掉django.middleware.csrf.CsrfViewMiddleware'。
post请求可以使用这种方式接收


本文探讨了Java直接调用Python脚本的局限性和问题,如路径依赖、环境配置复杂及调试困难。提出了通过Python搭建HTTP服务,使用Django框架,Java通过HTTP请求调用Python脚本的解决方案,有效解决了上述问题。

被折叠的 条评论
为什么被折叠?



