从PHP传递一条有效的Python路径。

时间:2021-12-25 17:04:16

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).

我有一个Python程序,它解析文件、获取路径as和参数并解析给定路径中的所有文件和所有子目录——使用os.walk(path)。我想从php Web应用程序调用它,这样用户就可以指定路径,然后将路径作为参数传递给解析器。(传递路径是可以的,因为它都在内部网络上)。

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.

我可以调用解析器,并使用popen()传递参数,但是Python程序接收的路径总是无效的。我已经让php脚本输出它发送给浏览器的命令。如果我将该命令复制并粘贴到命令窗口中,解析器就可以正常工作了。

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

我知道php脚本传递的路径从Python脚本中的os.path.exist (path)的结果来看是无效的

This is the code to call the Python program:

这是调用Python程序的代码:

$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"

python C:\工作区主要\解析器\ src \ \主要。py 2”我:\ 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.

其中2只是脚本的另一个参数,$_REQUEST['location']是从调用页面上的表单输入文本框中定义的。

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

这是在Windows系统上,所以我假设这与路径中的反斜线有关。

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().

基本上,我不确定如何处理所有的反斜杠。我想了解如何将包含反斜杠的字符串发送到php页面,以及如何使用popen()再次发送它们。我认为打印到浏览器的结果不是原始命令字符串,我不能确定popen()发出的命令中有多少回斜杠。

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

如果有人有什么想法,我将非常感激。

Edit:

编辑:

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

因此在Python程序中,路径的使用如下:

 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.

这将从$read变量回显到浏览器。

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)

这与从命令行运行命令(从浏览器复制并粘贴到cmd窗口的命令)的输出相同。但是,当以这种方式运行时,路径是真实的,并且所有的文件都被解析。(在命令窗口中,html标记也显示)

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

web服务器和解析器驻留在本地机器上。

1 个解决方案

#1


1  

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.

检查PHP服务器运行的是什么用户。如果I:\是一个网络驱动器,不要期望它们被映射到该用户下面。使用UNC路径代替。

Things to try:

尝试的东西:

  • a different path (we know C:\Workspaces\parsers\src\main\ works, why don't you try that?)
  • 一个不同的路径(我们知道C:\工作区解析器\src\main\工作,为什么不试试呢?)

#1


1  

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.

检查PHP服务器运行的是什么用户。如果I:\是一个网络驱动器,不要期望它们被映射到该用户下面。使用UNC路径代替。

Things to try:

尝试的东西:

  • a different path (we know C:\Workspaces\parsers\src\main\ works, why don't you try that?)
  • 一个不同的路径(我们知道C:\工作区解析器\src\main\工作,为什么不试试呢?)