A
A
altblog2020-09-15 16:11:27
PHP
altblog, 2020-09-15 16:11:27

Why does a PHP+Python script work in the server console, but not in the browser?

I can't get PHP to output lemmatized text in any way. Emptiness is displayed. Everything works in the terminal, but not in the browser. In this case, test.py is displayed, but the value from pystem.py is not.

test.py

text3 = 'привет'
print(text3.encode('utf-8'))


pystem.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from pymystem3 import Mystem
mystem = Mystem()
text = 'Поехали'
lemmas = mystem.lemmatize(text)
print(''.join(lemmas).encode('utf-8'))
print(text)


test.php
<?php
$python =  shell_exec('python3 test.py');
echo "printing: " .  $python; // выводит b'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82

$python2 =  shell_exec('python3 pystem.py');
echo "printing: " .  $python2; // ничего не выводит в браузер


Terminal:
root@214533:/var/www/html/panel/mystemtest# python3 test.py
b'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'

root@214533:/var/www/html/panel/mystemtest# python3 pystem.py
b'\xd0\xbf\xd0\xbe\xd0\xb5\xd1\x85\xd0\xb0\xd1\x82\xd1\x8c\n'


And even like this:
root@214533# php test.php
<br>pystem.py result: поехать


Maybe when I run from under the browser, it is not executed under root, but under www-data? I tried:
root@214533:/var/www/html/mystemtest# sudo -u www-data php test.php
Installing mystem to /root/.local/bin/mystem from http://download.cdn.yandex.net/mystem/mystem-3.1-linux-64bit.tar.gz
Traceback (most recent call last):
  File "pystem.py", line 7, in <module>
    mystem = Mystem()
  File "/usr/local/lib/python3.6/dist-packages/pymystem3/mystem.py", line 178, in __init__
    autoinstall()
  File "/usr/local/lib/python3.6/dist-packages/pymystem3/mystem.py", line 57, in autoinstall
    install(out)
  File "/usr/local/lib/python3.6/dist-packages/pymystem3/mystem.py", line 74, in install
    os.makedirs(MYSTEM_DIR)
  File "/usr/lib/python3.6/os.py", line 210, in makedirs
    makedirs(head, mode, exist_ok)
  File "/usr/lib/python3.6/os.py", line 220, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/root/.local'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2020-09-18
@SilenceOfWinter

root directory is different most likely, log/look for errors

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question