【问题标题】:How to run python3 on web server?如何在 Web 服务器上运行 python3?
【发布时间】:2016-11-12 05:17:48
【问题描述】:

我有这个 python 3 程序,但运行有问题。当我使用python3 4230.py 运行thought ssh 时,它的工作方式应该是这样(它会打印出数据),但是当我尝试像python 4230.py 那样运行它时,它给了我很多错误,因为它的PY3 程序。所以我想找到一种方法,让我拥有的这个 PY 脚本能够打印出答案。为了回显 python 在网站上打印的所有内容,我使用了这个 WP 插件:

<?php # -*- coding: utf-8 -*-
/* Plugin Name: 4230 */
header('Content-Type: text/html; charset=cp1252');
add_shortcode( '4230', 'execute_python_with_argv' );
function execute_python_with_argv(){
ob_start();
$description = array (     
0 => array("pipe", "r"),  // stdin
1 => array("pipe", "w"),  // stdout
);
$application_system = "python ";
$application_path .= plugin_dir_path( __FILE__ );
$application_name .= "4230.py";
$separator = " ";
$application = $application_system.$application_path.$application_name.$separator;
$pipes = array();
$proc = proc_open ( $application , $description , $pipes );
if (is_resource ( $proc ))
{
$var=  stream_get_contents ($pipes [1] ); //Reading stdout buffer
}
echo "<pre>".$var."</pre>";
$output = ob_get_clean();
return $output;
}

这段代码中不应该有任何语法错误,我在使用 WAMP 时对它和 python3 没有任何问题。但我认为这段代码应该激活python程序,所以也许可以让它发送请求让python运行python3

同样which python3 通过 ssh 打印出 /home/meteo/.local/bin/python3,我试图将此行添加到我的 python 脚本的顶部作为像这样的“shebang”#!/home/meteo/.local/bin/python3,但它并没有帮助运行我的 PY 脚本和python3 打印数据。

那么我应该怎么做才能让这个python脚本作为python3运行并打印出答案呢?

编辑:这是我在使用python 4230.py 运行 python 脚本时遇到的错误:

Traceback (most recent call last):
File "4230.py", line 4, in <module>
from   bs4 import BeautifulSoup
File "/home/meteo/public_html/wp-content/plugins/bs4/__init__.py", line 30, in <module>
from .builder import builder_registry, ParserRejectedMarkup
File "/home/meteo/public_html/wp-content/plugins/bs4/builder/__init__.py", line 4, in <module>
from bs4.element import (
File "/home/meteo/public_html/wp-content/plugins/bs4/element.py", line 8, in <module>
from bs4.dammit import EntitySubstitution
File "/home/meteo/public_html/wp-content/plugins/bs4/dammit.py", line 13, in <module>
from html.entities import codepoint2name
ImportError: No module named html.entities

EDITv2:用新的 WP 插件解决了这个问题:

<?php # -*- coding: utf-8 -*-
/* Plugin Name: viassh */   
header('Content-Type: text/html; charset=ANSI_X3.4-1968');
add_shortcode('viassh', 'HelloWorldShortcode');
function HelloWorldShortcode() {
ob_start();
$old_path = getcwd(); 
chdir('/home/meteo/public_html/wp-content/plugins/');
$output = shell_exec('./4230.py');
chdir($old_path);
echo $output;
$output = ob_get_clean();
return $output;
}

感谢阅读。

【问题讨论】:

  • 您尝试将$application_system = "python "; 更改为$application_system = "python3 "; 吗?
  • @MaximilianPeters 是的,我做了,没有帮助。
  • 你确定这是 Python3/Python2 的问题,而不是脚本调用方式的问题吗?不同的用户权限?命令行参数等?
  • @MaximilianPeters 我不确定,可能是这样,但是在我的 Windows PC 上,当我使用 WAMP 时,这个带有 python 脚本的 WP 插件按预期的方式工作。我应该尝试什么?
  • 将错误消息添加到您的问题中?还可以尝试用print "Hello, it's working" 之类的东西替换python 代码。这将在 Python2 中有效,但在 Python3 中无效,并且可以让您了解脚本是否可以正常工作。

标签: php python python-3.x centos6


【解决方案1】:

评论部分的扩展讨论摘要和更一般的问题“为什么我的 Python 脚本在从 PHP、cgi 等调用时没有运行?”


你能调用 Hello World 脚本而不是你的真实脚本吗?例如

#!/usr/bin/env python or python3
from sys import version    
print(version)
  • 如果是,那太好了,那么问题出在你真正的 Python 脚本上,而不是你调用它的方式。检查您是否可以从命令行运行脚本(没有简单的缩进错误,复制和粘贴中的截断行),最好是作为通过服务器启动时将执行脚本的用户(即不是作为 root/admin)。使用记录器查看脚本崩溃的位置。

  • 如果不是,请检查脚本的permissionsowner


在这种特殊情况下,将整个启动 Pytyhon 和脚本包装在一个 shell 文件中,并从您的服务器调用此文件。有点老套,但总比失去所有头发要好。

创建文件文件python_script.sh

echo "just a test to see if the script is executed, remove this line later"
python3 /absolute/path/to/script/python_script.py

使用chmod +x python_script.sh 确保文件为executable,然后通过CGI 或PHP 调用此脚本。

【讨论】:

  • 所以我应该制作激活我的python程序的wordpress插件也激活python_script.sh?我应该将此行添加到 wp 插件 $output = shell_exec('./python_script.sh'); 的顶部来做到这一点?
  • 要么调用 shell 脚本,要么调用 python 程序,两者都过分了
  • 哦,好的,所以我必须选择一个。我尝试使用您编写的程序向我显示版本,它显示 2.6.6,即使我使用了 #!/usr/bin/env python3(这非常重要,因为它告诉 PY 使用什么版本的编译器,对吗?)。我查了/usr/bin,看到这个目录里的人有文件python3,我没有,我只有pythonpython2.6python2.6-config,所以因为这里不存在这条线#!/usr/bin/env python3 不起作用?那么,也许我是按旧的方式安装的?
  • 也试试shell脚本,写python3的完整路径
  • 好的,我制作了 shell 脚本,通过 ssh 它的工作原理完全一样,而且我不知道如何通过 php 打印这个脚本,我打算在这里获取更多的 py 文件,所以我必须为他们每个人创建一个额外的文件,这只会减慢网站的速度。让我完全困惑的另一件事是which python3/home/meteo/.local/bin/python3 我添加到py 版本程序#!/home/meteo/.local/bin/python3 当我开始这样./4230.py 我得到版本3.4.3,但在我的网站上它说2.6.6,为什么会这样?我的意思是它的相同脚本...
【解决方案2】:

使 python 脚本可执行

chmod +x script.py


./script.py

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 2010-10-12
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2010-09-11
    • 2018-01-30
    相关资源
    最近更新 更多