2022年 11月 9日

html点击按钮调用python脚本

需求:点击web页面上的按钮 调用python脚本

原理:
  1. 点击按钮发送ajax请求
  2. 用node搭建服务器接收请求
  3. 再使用node中的child_process.exec()方法去调用python脚本
    tips:
    child_process.exe()的作用是执行命令行操作
    child_process.exec('python test.py')相当于在命令行中输入python test.py
    达到调用脚本的目的

HTML部分:

<body>
	<button class='btn'>click</button>
</body>
  • 1
  • 2
  • 3

JS给按钮注册事件(使用jquery):

<script src='./lib/jquery-1.12.4.min.js'></script>
<script>
	$('.btn').click(() => {
		//向nodejs搭建的服务器发送请求
		$.ajax({
	        type: 'post',
	        url: 'http://localhost:8080/change',
	        data: { name: 'zs', age: 20 },
	        success: function(data) {
	          console.log(data)
	        },
	        error: function(err) {
	          console.log(err)
	        }
	      })
		})
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Nodejs部分(index.js):需要安装用到的相关依赖

//需要下载
const express = require('express')
//需要创建router.js文件
const router = require('./router')
//需要安装
const bodyParser = require('body-parser')
//引用 用于发送命令行命令
const child_process = require('child_process')
//创建一个服务器
const app = express()
//设置需要使用模板
app.engine('html', require('express-art-template'))
//设置模板目录
app.set('views', 'pages')
//托管静态资源
app.use('/assets', express.static('assets'))
//给req.body赋值
app.use(bodyParser.urlencoded())
//处理路由
app.use(router)
//接收发送来的post请求
app.post('/change', function(req, res) {
//调用python脚本 同理于在命令行运行python脚本
//调用语句:python test.py 在后面拼接参数向脚本中传参
  var workerProcess = child_process.exec(
    `python test.py ${req.body.name} ${req.body.age}`,
    function(error, stdout, stderr) {
      if (error) {
        console.log(error.stack)
        console.log('Error code: ' + error.code)
        console.log('Signal received: ' + error.signal)
      }
      console.log('stdout: ' + stdout)
      // console.log('stderr: ' + stderr)
    }
  )
  workerProcess.on('exit', function(code) {
    // console.log('exit code ' + code)
  })
  //打印接收到的data数据
  console.log(req.body)
  res.json(req.body)
})

// 启动服务器
app.listen(8080, () => console.log('http://localhost:8080 server start'))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
router.js部分:
  • 1
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const child_process = require('child_process')

const router = express.Router()

router.get('/demo', (req, res) => {
  res.sendFile(path.join(__dirname, 'pages', 'demo.html'))
})
router.get('/', (req, res) => {
  res.redirect('/demo')
})

module.exports = router
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
test.py部分:
  • 1
print('hello world')
  • 1

结构

  1. 打开powershell 安装依赖
    • cnpm i express
    • cnpm i express-art-template
    • cnpm i art-template
  2. 运行index.js
    • node index.js
  3. 显示:http://localhost:8080 server start 服务器开启成功
  4. 在浏览器地址栏中输入http://localhost:8080会自动跳转到demo.html页面
  5. 点击button 就可以调用python脚本