Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

php - 如何在python中使用参数调用PHP脚本(How can I call PHP script with arguments in python)

import subprocess

(导入子流程)

V1 = 1 #argument1

(V1 = 1#argument1)

V2 = 2 #argumeng2

(V2 = 2#argumeng2)

Output = subprocess.call(["bin/sudo -u username /bin/PHP pathtofilename.php V1, V2"])`

(输出= subprocess.call([[“ bin / sudo -u username / bin / PHP pathtofilename.php V1,V2”])`)

But my php script is not getting executed.

(但是我的PHP脚本没有执行。)

Kindly help.

(请帮助。)

  ask by kotesh translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem could be in the way you are passing arguments to subprocess.call

(问题可能在于您将参数传递给subprocess.call的方式)

The function's signature is

(该函数的签名是)

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None)

From documentation :

(从文档 :)

args is required for all calls and should be a string, or a sequence of program arguments.

(args是所有调用所必需的,并且应为字符串或程序参数序列。)

Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (eg to permit spaces in file names).

(通常最好提供一个参数序列,因为它允许模块处理任何必需的参数转义和引用(例如,允许在文件名中保留空格)。)

If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

(如果传递单个字符串,则外壳程序必须为True(请参见下文),否则该字符串必须简单地命名要执行的程序而无需指定任何参数。)

So your code should be something like this:

(因此,您的代码应如下所示:)

Output = subprocess.call(["bin/sudo", "-u", "username", "/bin/PHP", "pathtofilename.php", "V1", "V2"])

Alternatively it could be

(或者,它可能是)

Output = subprocess.call(["bin/sudo -u username /bin/PHP pathtofilename.php V1 V2"], shell=True)

but you should consider possible security issues .

(但是您应该考虑可能的安全问题 。)

Note: If you are on Python 3.5+ consider using subprocess.run , especially if you want to capture stdout or stderr with a CompletedProcess instance.

(注意:如果您使用的是Python 3.5+,请考虑使用subprocess.run ,尤其是当您想使用CompletedProcess实例捕获stdout或stderr时。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...