python - 如何部署在 Azure 中运行 python 脚本的 node.js 应用程序?
问题描述
我正在尝试部署一个 node.js 应用程序,该应用程序为后台任务调用 python 脚本。我实现它的方式是通过python-shell:
var pythonShell = require('python-shell');
var options = {
pythonPath: 've-env/bin/python3',
args:
[
req.query.term,
req.params.id,
req.session.user.searchName,
req.session.user.searchKey
]
};
pythonShell.run('VideoAnalyzer/Search.py', options, function (err, data) {
if (err)
throw err ;
var values = JSON.parse(data[0]).value;
var resultsByRel = values;
res.render('video', {resultsByRel: resultsByRel, resultsByTime: [], searchTerm: req.query.term, url: req.query.url});
});
python 的路径在 options.pythonPath 中指定(在名为 've-env' 的 python 虚拟环境中)。
这适用于我的本地环境。但是,当我将我的应用部署到 Azure 应用服务时,我收到以下错误消息:
Error: spawn Unknown system error -8
at _errnoException (util.js:992:11)
at ChildProcess.spawn (internal/child_process.js:323:11)
at exports.spawn (child_process.js:502:9)
at new PythonShell (/home/site/wwwroot/node_modules/python-shell/index.js:59:25)
at Function.PythonShell.run (/home/site/wwwroot/node_modules/python-shell/index.js:160:19)
at Object.exports.search_result_video (/home/site/wwwroot/controllers/searchController.js:20:15)
at /home/site/wwwroot/routes/video.js:15:21
at Layer.handle [as handle_request] (/home/site/wwwroot/node_modules/express/lib/router/layer.js:95:5)
at next (/home/site/wwwroot/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/site/wwwroot/node_modules/express/lib/router/route.js:112:3)
该应用程序部署在 Linux 环境中,节点版本为 v8.9。
在部署之前我应该执行任何 python 环境配置吗?
解决方案
在部署之前我应该执行任何 python 环境配置吗?
答案是肯定的。如果你想在 Azure 应用服务中运行 python 脚本,你需要在你的应用程序中安装 python 环境。请参考以下步骤:
请参考我的工作步骤,看看错误是否仍然出现。
第 1 步:在门户上添加扩展(这里是 Python 3.6.1 x64)
步骤2:切换到Kudu CMD和命令cd Python364x64
并将touch get-pip.py
url的内容复制https://bootstrap.pypa.io/get-pip.py
到get-pip.py
via Edit按钮中,然后运行python get-pip.py
安装pip工具。
第 3 步:通过以下方式安装您需要的任何软件包python -m pip install requests
然后你需要修改你的代码的python配置:
var express = require('express');
var pythonShell = require('python-shell');
var router = express.Router();
var options = {
pythonPath: 'D:/home/python364x64/python',
scriptPath: 'D:/home/site/wwwroot'
// args:
// [
// req.query.term,
// req.params.id,
// req.session.user.searchName,
// req.session.user.searchKey
// ]
};
var resultsByRel;
pythonShell.run('TestRequests.py', options, function (err, data) {
if (err) throw err;
// results is an array consisting of messages collected during execution
var values = JSON.parse(data[0]);
resultsByRel = values;
console.log('results: %j', resultsByRel);
});
router.get('/', function(req, res, next) {
res.send(resultsByRel);
// res.render('executePython', resultsByRel );
});
module.exports = router;
我的简单python脚本:
import requests
r= requests.get("https://www.bing.com")
print (r.status_code)
希望它可以帮助你。有任何疑问,请告诉我。
推荐阅读
- mysql - SQL - 将 2 个表与日期进行比较
- c++ - 如何在 C++ 中将 OID_PM_WOL_PATTERN_LIST 发送到 NDIS 驱动程序?
- ios - 带 Swift 的步进滑块
- javascript - 传单从离线地图中删除旧/错误图块
- java - 使用 Apache Cordova + Electron 在 Windows 上访问 Java 库
- concurrency - 具有不同 URL 和数据的多个 http 请求
- c++ - 如何从输入中获取字符串向量?
- java - 配置 Logstash 以从 socket 接收数据并将其插入 java 中的 Elasticsearch
- python - SQL-Alchemy 重用列定义
- automated-tests - 当我尝试从 testcafe 脚本访问外部 URL 时出现 500 内部服务器错误