Search code examples
phppythonpython-3.xcentos6

How to run python3 on web server?


I have this python 3 program that I have problem running. When I run thought ssh using python3 4230.py it works like it should(it prints out data), but when I try to run it like python 4230.py it gives me lots of errors because its PY3 program. So I want to find a way on how could I make that this PY script I have, would print out the answers. To echo everything python prints on website, I am using this WP plugin:

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

There should not be any syntax errors in this code, I didn't had any problem with it and python3 when I was working on WAMP. But I though that this code should activate python program, so maybe it is possible to make it send request for python to run with python3?

Also as which python3 through ssh prints out /home/meteo/.local/bin/python3, I have tried to add this line to the top of mine python script as a "shebang" like this #!/home/meteo/.local/bin/python3 but it didn't help running my PY script and python3 to print out the data.

So what should I do to make this python script to run as python3 and print out the answer?

EDIT: This is the error I get when I run python script with 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: Fixed this problem with new WP plugin:

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

Thanks for reading.


Solution

  • A summary of the extended discussion in the comment section and a more general question to 'why is my Python script not running when called from PHP, cgi, etc.?'


    Can you call a Hello World script instead of your real script? e.g.

    #!/usr/bin/env python or python3
    from sys import version    
    print(version)
    
    • If yes, great, then the problem is in your real Python script and not not the way you call it. Check if you can run the script from the command line (no simple indentation errors, truncated lines from copy&paste), preferably as the user who will execute the script when started via the server (i.e. not as root/admin). Use a logger to see where the script crashes.

    • If no, check the permissions and owner of the script.


    In this particular case, wrap the whole starting Pytyhon and script, in a shell file and call this file from your server. A bit hacky but better than losing all your hair about it.

    create a file file 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
    

    make sure the file is executable by using chmod +x python_script.sh and then call this script via CGI or PHP.