【问题标题】:How to run Python script with correct permissions in PHP?How to run Python script with correct permissions in PHP?
【发布时间】:2022-12-19 08:54:46
【问题描述】:

I can run below script to print lines from the file in PHP so it looks like PHP has appropriate permissions but I'm not entirely sure. I also tried to make script executable, but no change. Below script works from shell and it writes passed argument to file. What do I miss?

Python script:

#!/usr/bin/python3
import sys
ip = sys.argv[1]
with open('/var/www/public_html/images/.htaccess') as file:
    lines = file.readlines()
    print(lines)
    if not ip in file.read():
        lines = [line.rstrip() for line in lines]
        lines.insert(-1,'    Require ip '+ip)
        with open('/var/www/public_html/images/.htaccess','w') as f:
            for a in lines:
                f.write(a+'\n')

PHP script:

  $ip = $_SERVER['REMOTE_ADDR'];
  $command = escapeshellcmd('/var/www/public_html/cgi-bin/test.py');
  $output = shell_exec($command).$ip;
  echo $output

【问题讨论】:

    标签: python php


    【解决方案1】:

    pass the $ip argument inside the escapeshellcmd(). Also if you suffer from bad permission issues try to provide your python script as an input to python interpreter:

      $ip = $_SERVER['REMOTE_ADDR'];
      $command = escapeshellcmd('/usr/bin/python3 /var/www/public_html/cgi-bin/test.py '.$ip);
      $output = shell_exec($command);
      echo $output
    

    Also, I don't think your php script has enough access rights to patch .htaccess file. If so - it's web server misconfiguration, so the idea of this script is very questionable.

    【讨论】:

    • my another issue was missing space after the test.py which was messing input in shell
    【解决方案2】:

    maybe your server dont have the wsgi module try to run on terminal:

    a2enmod wsgi
    

    【讨论】:

      猜你喜欢
      • 2022-12-26
      • 2022-12-28
      • 2022-11-09
      • 2022-12-26
      • 2022-12-27
      • 2022-12-19
      • 2022-12-02
      • 2022-12-27
      • 2022-12-19
      相关资源
      最近更新 更多