java平台要调用pyhon平台已有的算法,为了减少耦合度,采用pyhon平台提供restful 接口,java平台负责来调用,采用http+json格式交互。
public static string invok细节决定成败议论文ealgorithm(string url, hashmap params) throws exception { httpheaders headers = new httpheaders(); headers.tcontenttype(mediatype.parmediatype("application/json; chars工伤鉴定申请书et=utf-8")); headers.add("accept", mediatype.application_json.tostring()); httpentity<string> httpentity = new httpentity<>(jsonobject.tojsonstring(params), headers); resttemplate rst = new resttemplate(); responentity<string> stringresponentity = rst.postforentity(url, httpentity, string.class); return stringresponentity.getbody(); }
两个入参:url为python提供restful调用方法;params参数,项目中参数使用了map,然后将map转成了json,与python服务器约定json格式传输。
经过反复调研与深思熟虑的考虑后,决定采用flask提供rest接口, flask 是一款非常流行的python web框架,微框架、简洁,社区活跃等。(其实是因为安装的anaconda自带了flask,一配置一启动好了,就是这么巧)
# -*- coding: utf-8 -*-from flask import flask, request, nd_from_directoryfrom k_means import execapp = flask(__name__)import logging@app.route('/')def index(): return "hello, world!"# k-means算法@app.route('/getkmeansinfobypost', methods=['post'])def getkmeansinfobypost(): try: result = exec(request.get_json()) except indexerror as e: logging.error(str(e)) return 'exception:' + str(e) except keyerror as e: logging.error(str(e)) return 'exception:' + str(e) except valueerror as e: logging.error(str(e)) return 'exception:' + str(e) except exception as e: logging.error(str(e)) return 'exception:' + str(e) el: return result@app.route("/<path:filename>")def getimages(filename): return nd_from_directory(dirpath, filename, as_attachment=true)if __name__ == '__main__': app.run(host="0.0.0.0", port=5000, debug=true)
代码为真实项目示例,去掉了一些配置而已,示例中包含三个方法,分别说一下
# -*- coding: utf-8 -*-from flask import flaskapp = flask(__name__)@app.route('/')def index(): return "hello, world!"if __name__ == '__main__': app.run(host="0.0.0.0", port=5000, debug=true)
# -*- coding: utf-8 -*-from flask import flask, requestfrom k_means import execapp = flask(__name__)import logging# k-means算法@app.route('/getkmeansinfobypost', methods=['post'])def getkmeansinfobypost(): try: result = exec(request.get_json()) except indexerror as e: logging.error(str(e)) return 'exception:' + str(e) except keyerror as e: logging.error(str(e)) return 'exception:' + str(e) except valueerror as e: logging.error(str(e)) return 'exception:' + str(e) except exception as e: logging.error(str(e)) return 'exception:' + str(e) el: return resultif __name__ == '__main__': app.run(host="0.0.0.0", port=5000, debug=突然之间歌词true)
说明:1.接收post方法;2. 从request获取java传过来的参数,对应上面的java调西班牙超级杯直播用代码
# -*- coding: utf-8 -*-from flask import flask, nd_from_directoryapp = flask(__name__)@app.route("/<path:filename>")def getimages(filename): return nd_from_directory(dirpath, filename, as_attachment=true)if __name__ == '__main__': app.run(host="0.0.0.0", port=5000, debug=true)
说明:1.还是flask有关快乐的作文框架提供的:nd_from_directory
2.dirpath目录,一般可以给个固定存放目录,调用的时候只用给文件名称就可以直接下载对应文件。
nohup python restapi.py &
本文发布于:2023-04-05 04:16:54,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/8fa973f34fc643510c3821886da22ab5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:java执行python代码(java远程调用python脚本讲解).doc
本文 PDF 下载地址:java执行python代码(java远程调用python脚本讲解).pdf
留言与评论(共有 0 条评论) |