当前位置: 首页 > news >正文

nodejs + express 调用本地 python程序

假设已经安装好 nodejs ;

cd /js/node_js ; 安装在当前目录的 node_modules/

npm install express --save 

或者 cnpm install express --save

web 服务器程序 server.js

const http = require('http');
const express = require('express');
const path = require('path');
const app = express();
const exec = require('child_process').exec;
var server_info = '\nServer running at http://127.0.0.1:8000/  (CTRL+C to stop)';function run_command(command) {console.log("\n" + command);exec(command, (err, stdout, stderr) => console.log(stdout, server_info));
}
// 静态文件路径
app.use(express.static('public'));app.get('/', function(req, res) {res.sendFile(path.join(__dirname + '/index.html'));
});app.get('/draw_flower1', function(req, res) {res.sendFile(path.join(__dirname + '/index.html'));res.redirect('/');run_command("pythonw /python/draw_flower1.py");
});app.get('/draw_rose1', function(req, res) {res.sendFile(path.join(__dirname + '/index.html'));res.redirect('/');run_command("pythonw /python/draw_rose1.py");
});app.get('/draw_xilan1', function(req, res) {res.sendFile(path.join(__dirname + '/index.html'));res.redirect('/');run_command("pythonw /python/draw_xilan1.py");
});app.listen(8000, "127.0.0.1", function() {console.log(server_info);
});

index.html  用 jQuery.ajax 读取文本文件

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>python draw flowers</title><script src="jquery-3.2.1.min.js"></script>
</head>
<body><div class="container">turtle 画一朵花    <a href="#" onclick="readfile('draw_flower1.py');">源代码 </a><form method="put" action="/draw_flower1"><input type="submit" value="运行 draw_flower1.py"></form>turtle 画一朵玫瑰花 <a href="#" onclick="readfile('draw_rose1.py');">源代码 </a><form method="put" action="/draw_rose1"><input type="submit" value="运行 draw_rose1.py"></form>turtle 画一朵西兰花 <a href="#" onclick="readfile('draw_xilan1.py');">源代码 </a><form method="put" action="/draw_xilan1"><input type="submit" value="运行 draw_xilan1.py"></form></div><p><pre id="code"> </pre></p>
<script type="text/javascript">
/*** 利用 ajax 读取文本文件* @param {文件路径} url */function readfile(url){$.ajax({url: url, // 文本文件位置type: "GET", // 请求方式为 getasync: false,dataType: "text", // 返回数据格式为 textsuccess: function(data) { //请求成功完成后要执行的方法 $('#code').html(data);}})}
</script>
</body>
</html>

jquery.min.js , python 程序要放在 public/

运行 node server.js

浏览器访问 http://127.0.0.1:8000/

参考旧版本:python : html 调用本地python程序

http://www.lryc.cn/news/102434.html

相关文章:

  • 微信小程序代码优化3个小技巧
  • 某行动态cookie反爬虫分析
  • 恒运资本:A股、港股全线爆发,沪指突破3300点,恒指重返2万点上方
  • Rust vs Go:常用语法对比(十二)
  • jmeter接口测试、压力测试简单实现
  • PysparkNote006---pycharm加载spark环境
  • 19套项目实战系列--Spring Cloud Spring Boot(整套源码)
  • TCP/IP协议详解(二)
  • Linux6.2 ansible 自动化运维工具(机器管理工具)
  • 前端面试题 —— React (二)
  • 【分享帖】LCD的MCU接口和SPI接口详解
  • 【Java】使用@Expose注解和excludeFieldsWithoutExposeAnnotatGson()方法将toJson()过程的部分字段忽略
  • 移动硬盘不显示怎么办?正确解决方式看这里!
  • MySQL 5.7.39 关于时间精度
  • 宝塔设置云服务器mysql端口转发,实现本地电脑访问云mysql
  • centos下安装ftp-读取目录列表失败-
  • 0101sub-process /usr/bin/dpkg returned an error code-dpkg-linux问题集
  • 流控平台Sentinel搭建和接入教程
  • 使用 docker 一键部署 MongoDB
  • 【深度学习】Inst-Inpaint: Instructing to Remove Objects with Diffusion Models,指令式图像修复
  • 创建维基WIKI百科和建立百度百科有何不同?
  • Python小红书旋转验证码识别
  • ELK搭建
  • webyog最新社区版免费版下载地址
  • [SQL挖掘机] - 窗口函数 - dense_rank
  • stable diffusion
  • web3行业有哪些职业发展路径?
  • MATLAB算法实战应用案例精讲-【自动驾驶】相控阵天线方向图
  • ALLEGRO之View
  • 【打造超酷的GitHub主页】