Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a valid path to Python from PHP

Tags:

python

php

I have a Python program that parses files, takes a path as and argument and parses all files in the given path and all sub directories - using os.walk(path). I want to call this from my php Web App, so the user can specify a path, which is then passed as an argument to the parser. (Passing a path is ok because its all on an internal network).

I can call the parser fine and pass the arguments ok using popen(), but the path that the Python program receives is always invalid. I have had the php script output the command it is sending to the browser. If I copy and paste that command into a command window, the parser works fine.

I know the path the php script passes is invalid from the result of os.path.exists(path) in the Python script

This is the code to call the Python program:

$path = $_REQUEST['location'];
echo "Path given is: ".$path;
$command = 'python C:\Workspaces\parsers\src\main\main.py '. intval($mode).'  "'.$path.'"';
echo "<p>".$command."</p>";
$parser = popen($command, 'r');
if ($parser){
  echo "<p>Ran the program</p>";
  while (!feof($parser)){
    $read = fgets($parser);
    if (!$read)
      echo "<p>Reached end of file</p>";        
      else
        echo "<p>".$read."</p>";                 
    }
  }

The command echoed in the browser is like:

python C:\Workspaces\parsers\src\main\main.py 2 "I:\Dir1\Dir2\Dir3"

Where the 2 is just another argument to the script and $_REQUEST['location'] is defined from an input text box in a form on the calling page.

This is on a Windows system, so I am assuming this has something to do with the backslashes in the path.

Basically, I am unsure as to how all the backslashes are being handled. I would like to understand how strings containing backslashes are sent to the php page, and how they are sent again using popen(). I think the result being printed to the browser is not the raw command string, and I can't be sure how many backslashes are really in the command being issued by popen().

If anyone has any ideas I'd really appreciate it.

Edit:

So in the Python program the path is used as follows:

 nfiles=0
 print 'Parsing all files in directory tree '+path+"<br />"
 start = time.time()
 if not os.path.exists(path):
   print "<p>Path is NOT REAL!!!</p>"
 else:
   print "<p>Path IS real!</p>"
 for root, dirs, files in os.walk(path):
   for f in files:
     file = os.path.join(root,f)
     print file
     nfiles+=1
     ...Code to run parser...
 print nfiles, "Files parsed<br />"

This is echoed back to the browser from the $read variable.

Output of that is:

Parsing all files in directory tree I:\Dir1\Dir2\Dir3
Path is NOT REAL!!!

0 Files parsed

This is identical to the output if the command is run from the command line (the command being copied from the browser and pasted into the cmd window). EXCEPT, when run that way the path IS real, and all the files are parsed. (and in the command window the html markup shows too)

The web server and parsers are hosted on my local machine.

like image 276
K4KYA Avatar asked Oct 06 '11 11:10

K4KYA


1 Answers

Check to see what user the PHP server is running as. If I:\ is a network drive, don't expect those to be mapped under that user. Use a UNC path instead.

Things to try:

  • a different path (we know C:\Workspaces\parsers\src\main\ works, why don't you try that?)
like image 169
Daren Thomas Avatar answered Oct 20 '22 00:10

Daren Thomas