【发布时间】: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