<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div id="appendonsend"></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div id="divRplyFwdMsg" dir="ltr">On 28/09/20 17:51, roberta fagandini wrote:<br>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt">
<div class="PlainText">> Sorry, maybe I didn't explain myself well because actually, I don't get
<br>
> any error from the php web page, simply the code related to grass is not <br>
> executed.For instance gsetup.init(gisbase, gisdb, location, mapset) does <br>
> not create any file in my /tmp/ folder and the print(gscript.gisenv()) <br>
> is not printed.<br>
> I checked the import of the grass libraries (print(sys.modules)) and <br>
> they seem to be correctly imported.<br>
<br>
Have you checked your webservers error logs ?</div>
<div class="PlainText"><br>
</div>
<div class="PlainText">I have already checked and this is the error in the webserver log file</div>
<div class="PlainText"><br>
</div>
<div class="PlainText">File "importgrass.py", line 82, in <module>
<div> main()</div>
<div> File "importgrass.py", line 71, in main</div>
<div> rcfile = gsetup.init(gisbase, gisdb, location, mapset)</div>
<div> File "/usr/lib/grass74/etc/python/grass/script/setup.py", line 170, in init</div>
<div> config_dir = os.path.join(os.getenv('HOME'), config_dirname)</div>
<div> File "/usr/lib/python3.6/posixpath.py", line 80, in join</div>
<div> a = os.fspath(a)</div>
<div>TypeError: expected str, bytes or os.PathLike object, not NoneType</div>
<br>
<br>
How do you call the python script from PhP?</div>
<div class="PlainText"><br>
</div>
<div class="PlainText">I tried both with:</div>
<div class="PlainText"><?php</div>
<div class="PlainText"> $command = escapeshellcmd('/usr/bin/python3 importgrass.py ');</div>
<div class="PlainText"> echo exec($command, $output, $return);</div>
<div class="PlainText">?></div>
<div class="PlainText"><br>
</div>
<div class="PlainText">and</div>
<div class="PlainText"><br>
</div>
<div class="PlainText">
<div class="PlainText" style="margin: 0px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255)">
<?php</div>
<div class="PlainText" style="margin: 0px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255)">
$command = escapeshellcmd('/usr/bin/python3 importgrass.py ');</div>
<div class="PlainText" style="margin: 0px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255)">
echo system($command, $return);</div>
<div class="PlainText" style="margin: 0px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255)">
?></div>
<div class="PlainText" style="margin: 0px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255)">
<br>
</div>
<div class="PlainText" style="margin: 0px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255)">
and </div>
<div class="PlainText" style="margin: 0px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255)">
<br>
</div>
<div class="PlainText" style="margin: 0px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255)">
<?php</div>
$intestazione='#!/bin/bash'.PHP_EOL; </div>
<div class="PlainText"> $command = escapeshellcmd('/usr/bin/python3 importgrass.py ');
<div> $myfile = fopen("./tmp/script.sh", "w") or die("Unable to open file!");</div>
<div> fwrite($myfile, $intestazione);</div>
<div> fwrite($myfile, $command);</div>
<div> fclose($myfile);</div>
<div> echo exec("/bin/sh ./tmp/script.sh", $output, $return);</div>
print_r($output);<br>
?></div>
<div class="PlainText"><br>
Are you sure that your web server has sufficient permissions to access <br>
the relevant directories ?</div>
<div class="PlainText"><br>
</div>
<div class="PlainText">Yes, I'm pretty sure! If I run the python script from terminal as www-data it works perfectly. <br>
<br>
Moritz</div>
<div class="PlainText"><br>
</div>
<div class="PlainText">Thanks!</div>
<div class="PlainText">Roberta<br>
<br>
> ------------------------------------------------------------------------<br>
> *Da:* Moritz Lennert <mlennert@club.worldonline.be><br>
> *Inviato:* luned́ 28 settembre 2020 17:29<br>
> *A:* roberta fagandini <robifagandini@hotmail.it>; <br>
> grass-user@lists.osgeo.org <grass-user@lists.osgeo.org><br>
> *Oggetto:* Re: [GRASS-user] exwcuting grass python script from php web page<br>
> On 28/09/20 12:10, roberta fagandini wrote:<br>
>> Hi all!<br>
>> I wrote a python script with grass to be run from a php web page.<br>
>> The python script works perfectly if I run it from the terminal but when <br>
>> I try to run it from the php web page, the grass related part seems to <br>
>> be ignored and the grass session is not launched. I set the grass gis <br>
>> environment following this link [0].<br>
>> From the php side, I try to launch the python script using both exec(), <br>
>> system() and also a bash script to run the python script but I always <br>
>> got the same error.<br>
> <br>
> Maybe you could tell us what error you get ?<br>
> <br>
> Moritz<br>
<br>
</div>
</span></font></div>
</body>
</html>