如何在网络服务器上 运行 python3?

How to run python3 on web server?

我有这个 python 3 程序,但我遇到了问题 运行ning。当我 运行 认为 ssh 使用 python3 4230.py 时它工作正常(它打印出数据),但是当我尝试 运行 它像 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 程序,所以也许可以使用 python3?

将 python 的请求发送到 运行

此外,由于 which python3 通过 ssh 打印出 /home/meteo/.local/bin/python3,我尝试将此行作为 "shebang" 添加到我的 python 脚本的顶部 #!/home/meteo/.local/bin/python3 但它对 运行 宁我的 PY 脚本和 python3 打印数据没有帮助。

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

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

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;
}

感谢阅读。

评论部分的扩展讨论摘要和更一般的问题'why is my Python script not running when called from PHP, cgi, etc.?'


您可以调用 Hello World 脚本而不是您的真实脚本吗?例如

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

  • 如果没有,请检查脚本的permissions and owner


在这种特殊情况下,将整个起始 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 脚本可执行

chmod +x script.py


./script.py