spring调Python实时接口

@RequestMapping(value="ask")
    @ResponseBody
    public JSONObject queryJ(@RequestParam("company_id") String company_id ,
    	   HttpServletRequest request){
    	String json = ""  ; 
		try {
			Process pr = Runtime.getRuntime().exec("python "  + _getPath(request, "test.py ") + company_id) ;
			BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream() , "utf-8"));
			String line;
			while ((line = in.readLine()) != null) {
				json += line ;
			}
			in.close();
			pr.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JSONObject jo = JSONObject.parseObject(json) ;
		System.out.println(jo.getLong("company_id")) ;
		System.out.println(jo.getString("organization_code")) ;
		return jo ;
    }
    
	private String _getPath(HttpServletRequest request , String pyFileName){
		String needDbPath = request.getSession().getServletContext().getRealPath("/");
		needDbPath += "pythonFiles"+ File.separator +pyFileName ;
		return needDbPath ;
	}


#coding=utf-8
import psycopg2
import sys
import demjson
reload(sys)
sys.setdefaultencoding('utf8')

def gao():
   company_id = sys.argv[1] ;
   conn = psycopg2.connect(database="", user="", password="", host="", port="") ;
   cur = conn.cursor() ;
   cur.execute("select company_id , organization_code  , company_full_name  from ebd_coding_company_name where company_id =  " + company_id ) ;
   rows = cur.fetchall()[0] ;
   company_id =  rows[0]  ;
   organization_code = rows[1]   ;
   company_full_name =  rows[2]
   conn.close() ;
   data = {}
   data["company_id"] = company_id
   data["organization_code"] = organization_code
   return  demjson.encode(data)


if __name__ == '__main__':
   print gao()






{"company_id":20,"organization_code":"675031462"}





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值