简体   繁体   English

通过 PHP 脚本运行 python 脚本不起作用

[英]Running python script through PHP script not working

So basically, I am trying to run some preliminary tests for a website I will be building.所以基本上,我正在尝试为我将要构建的网站运行一些初步测试。 There will be downloads through the site, possibly for the same resource by different users, possibly nearly or at the same time.将通过站点进行下载,可能由不同的用户下载相同的资源,可能几乎或同时。 I want to implement a lock manager of sorts to prevent repeated downloads of a resource when one is already in progress.我想实现一种锁定管理器,以防止在一个资源已经在进行中时重复下载资源。

The test I am running is just to see if this is even possible.我正在运行的测试只是为了看看这是否可能。 What I am specifically testing for right now is if I begin running a program, if I attempted to open the program again would it open a completely new instance or go to the already open instance.我现在专门测试的是,如果我开始运行一个程序,如果我试图再次打开该程序,它会打开一个全新的实例还是转到已经打开的实例。 I am doing to this to try and see if user 1 makes changes in their program, if the second user opens their program, they will see those change;我这样做是为了尝试查看用户 1 是否在他们的程序中进行了更改,如果第二个用户打开他们的程序,他们将看到这些更改; otherwise they might not see the changes if they open up a completely new instance of a program.否则,如果他们打开一个全新的程序实例,他们可能看不到更改。

PHP PHP

exec(escapeshellcmd("C:\Program Files\Python 3.7\python.exe Test2Php.py 0 Testing"), $o1, $r);
echo $r;
var_dump($o1);

Python Python

#!/usr/bin/env python

import sys

arr = []

t = sys.argv[1]
if (t == '0'):
arr = [sys.argv[k] for k in range(2, len(sys.argv))]
print("if")
else:

print(str(len(arr)))

The problem is the script doesn't return any output at all!问题是脚本根本不返回任何输出! It doesn't run either.它也不运行。 I tested this by having the python program write a file at the end of successful execution.我通过让 python 程序在成功执行结束时写一个文件来测试这一点。 I have tried shell_exec, passthru, etc. The program itself works when run through command line, but not in any scripts I have made.我尝试过 shell_exec、passthru 等。程序本身在通过命令行运行时可以工作,但在我制作的任何脚本中都不起作用。

I am using a WAMP server on windows.我在 Windows 上使用 WAMP 服务器。

EDIT:编辑:

For anyone else dealing with this.对于其他处理此问题的人。 Make sure you have the default Python package in your system path variable.确保您的系统路径变量中有默认的 Python 包。 You can do this easily by installing the latest version of python and choosing add to system path.您可以通过安装最新版本的 python 并选择添加到系统路径来轻松完成此操作。 Uninstall anaconda or whatever else may be in the way of the system path and enjoy.卸载 anaconda 或其他任何可能妨碍系统路径的东西并享受。 Also make sure you find where the python exe is and use the full path to it.还要确保找到 python exe 所在的位置并使用它的完整路径。

Your list comprehension will get out of range since you can never do lst[len(lst)] without getting an IndexError .您的列表理解将超出范围,因为您永远无法在没有得到IndexError情况下执行lst[len(lst)] The str() wrapper isn't necessary to print len(arr) . str()包装器不需要打印len(arr)

Instead, use a slice of [:2] to remove the first 2 elements:相反,使用[:2]的切片删除前 2 个元素:

#!/usr/bin/env python

import sys

arr = []

t = sys.argv[1]

if t == '0':
    arr = sys.argv[2:]
    print("if")
else:
    print(len(arr))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM