如何在Web服务器上运行python3?

时间:2016-07-09 22:40:58

标签: php python python-3.x centos6

我有这个python 3程序,我运行有问题。当我使用python3 4230.py运行思考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脚本的顶部作为&#34; shebang&#34;像这样#!/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;
}

感谢阅读。

2 个答案:

答案 0 :(得分:1)

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

您可以调用 Hello World 脚本而不是真实脚本吗? e.g。

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

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

在这种特殊情况下,将整个起始Pytyhon和脚本包装在一个shell文件中,并从服务器调用此文件。有点hacky但比失去你所有的头发更好。

创建文件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调用此脚本。

答案 1 :(得分:0)

使python脚本可执行

chmod +x script.py


./script.py