0

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.

Jaxian
  • 1,146
  • 7
  • 14
MakeMeWise
  • 31
  • 1
  • 7
  • Did you try changing `$application_system = "python ";` to `$application_system = "python3 ";` ? – Maximilian Peters Jul 10 '16 at 08:49
  • @MaximilianPeters Yeah, I did, didn't help. – MakeMeWise Jul 10 '16 at 08:53
  • Are you sure it is a Python3/Python2 issue and not a problem of how the script is called? Different user rights? Command line arguments, etc? – Maximilian Peters Jul 10 '16 at 08:55
  • @MaximilianPeters I am not sure, it could be, but on my windows PC, when I was using WAMP this WP plugin with python script worked the way they are intended to work. What should I try? – MakeMeWise Jul 10 '16 at 09:21
  • Add the error messages to your question? Also try replacing the python code with something like `print "Hello, it's working" `. This will work in Python2 but not in Python3 and will give you some idea if the script is working at all. – Maximilian Peters Jul 10 '16 at 09:25
  • @MaximilianPeters I have added the errors I get when running with python2. I did try `print("hello world")` and this one too `import requests payload = {'key1': 'value1', 'key2': ['value2', 'value3']} r = requests.get('http://httpbin.org/get', params=payload) print(r.url) ` and they do work. – MakeMeWise Jul 10 '16 at 09:37
  • How did you install beautifulSoup? via pip? The error message "ImportError: No module named html.entities" has a couple of solutions/suggestions on SO. – Maximilian Peters Jul 10 '16 at 09:44
  • @MaximilianPeters Yes, I have installed it with pip, majority of the fixes were to use python3 instead of python and it works with python3, but how do I make so that program would be executed as python3 by default? – MakeMeWise Jul 10 '16 at 10:08
  • A bit hacky: Call a shell script in your PHP code which calls python3 and your script. – Maximilian Peters Jul 10 '16 at 10:11
  • @MaximilianPeters You mean in wp plugin I posted above? How can that be done? – MakeMeWise Jul 10 '16 at 10:21
  • http://stackoverflow.com/questions/11052162/run-bash-command-from-php and in your your script /home/meteo/.local/bin/python3 4230.py, then chmod +x – Maximilian Peters Jul 10 '16 at 10:27
  • @MaximilianPeters Ok, thanks for help. – MakeMeWise Jul 10 '16 at 10:51

2 Answers2

1

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.

Community
  • 1
  • 1
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
  • So I should make wordpress plugin which a activates my python program also activate python_script.sh? I should add this line to the top of wp plugin `$output = shell_exec('./python_script.sh');` to do that? – MakeMeWise Jul 10 '16 at 13:11
  • Either call the shell script or the python program, both would be an overkill – Maximilian Peters Jul 10 '16 at 13:13
  • Oh, ok, so I have to choose one. I have tried to use the program you have written to show me version it says `2.6.6` even tho I have used `#!/usr/bin/env python3` (this is very important because it says to PY what version compiler to use, right?). I have checked `/usr/bin` and I saw that people in this directory have file `python3`, I don't have it, I have only `python`, `python2.6` and `python2.6-config`, so because it doesn't exist here this line `#!/usr/bin/env python3` doesn't work? So maybe I installed it the worng way? – MakeMeWise Jul 10 '16 at 13:42
  • Try the shell script as well and write the full path to python3 – Maximilian Peters Jul 10 '16 at 14:14
  • Ok, I made shell script and through ssh it works totally the same, and tbh I have no clue on how to get this script printed through php and I am planning to get way more of py files here, so I would have to create for each of them an additional file that would just slow down the website. The other thing that totally confused me is `which python3` is `/home/meteo/.local/bin/python3` I added to py version program this `#!/home/meteo/.local/bin/python3` when I start like this `./4230.py` I get version 3.4.3, but on my website it says 2.6.6, why is this happening? I mean its the same script... – MakeMeWise Jul 10 '16 at 15:19
  • Did you try the solution [here](http://stackoverflow.com/q/11052162/2776376)? The performance loss is minimal, no need to worry about that. – Maximilian Peters Jul 10 '16 at 17:24
  • I just basically rewrite all wp plugin, what I did was instead of setting `$output = shell_exec('./python_script.sh');` I made `$output = shell_exec('./4230.py');` and it works! But now I have other problem, if data contains letters like `Ąčęėįšųūž` then nothing gets printed. I had problem like this before and solution was to find python encoding and it is `ANSI_X3.4-1968` and add this line at the top of the plugin `header('Content-Type: text/html; charset=ANSI_X3.4-1968');` but now I did that and characters still does't work, maybe you know how this could be fixed? I added my new plugin at OP – MakeMeWise Jul 10 '16 at 20:26
0

make the python script executable

chmod +x script.py


./script.py
ThunderHorn
  • 1,975
  • 1
  • 20
  • 42