I am connected to my OracleLinux server via an SSH connection. The server is running a working apache webserver and also php. Python3 is also installed.
Now I am currently trying to run a php script via a JavaScript AJAX request to in turn run a Python script with it.
Directory structure:
/var/www/website/webwatcher
--index.html
--js/
-----script.js (run PHP script from here with AJAX request)
--php/
-----getWebsiteElementValue.php (run webscraper.py from here)
--python/
-----webscraper.py
script.js
:
window.onload = () => {
getWebsiteElementValue((data) => {
console.log(data);
});
}
function getWebsiteElementValue(success) {
let ajax = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
let ajaxURL = '/webwatcher/php/getWebsiteElementValue.php';
ajax.open('post', ajaxURL);
ajax.onreadystatechange = () => {
if (ajax.readyState > 3 && ajax.status == 200) {
success(ajax.responseText);
}
}
ajax.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
ajax.send();
}
getWebsiteElementValue.php
:
<?php
$output = shell_exec('/usr/bin/python3 /var/www/website/webwatcher/python/webscraper.py 2>&1');
echo $output;
?>
webscraper.py
:
from bs4 import BeautifulSoup
from urllib.request import urlopen
from sys import argv
import ssl
url = 'https://buyzero.de/products/raspberry-pi-4-model-b-8gb?variant=31817426698342';
cssSelector = 'div.product-form__info-list > div:nth-child(2) > div > span';
context = ssl._create_unverified_context();
page = urlopen(url, context=context);
html = page.read().decode("utf-8");
soup = BeautifulSoup(html, "html.parser");
elementText = soup.select(cssSelector)[0].text;
print(elementText);
When I run the python script via the terminal, it returns the desired output without error.
However, when the same happens via the shell_exec() php command I get the following error messages (shortened) in the console of the website:
PermissionError: [Errno 13] Permission denied
and
urllib.error.URLError: <urlopen error [Errno 13] Permission denied>
The /var/www
folders both have the following permissions drwxr-xr-x.
and belong to root:root
.
The directory structure of website/
up to the file webscraper.py
(also all subfolders and files in it) belongs to apache:apache
and has the permissions drwxrwxr-x.
and -rwxrwxr-x.
respectively.
Where is the error now?
@MaciejWrobel's comment brought me to the solution of the problem.
Because the httpd_can_network_connect
boolean of the Linux system is set to off
by default, it seems to prevent access to various Python modules in a Python script and ends up in the above error. Running the command setsebool -P httpd_can_network_connect on
in the terminal fixed my problem.