简体   繁体   English

将变量从PHP传递到python

[英]Passing variables from PHP to python

I have a python script. 我有一个python脚本。 When I run the script from the command line as: 当我从命令行运行脚本时:

python upgrade.py arg1 arg2

The script runs successfully. 脚本成功运行。 Now I am trying to pass the same variables via PHP code using: 现在,我尝试使用以下方式通过PHP代码传递相同的变量:

passthru("python upgrade_device.py $arg1 $arg2");

The script does not execute. 脚本不执行。 I am accepting the arguments using: 我接受使用以下参数:

sys.argv

The arguments are passed correctly I have tested using print 我已经使用print测试了正确的参数

This is specific command where the execution fails: 这是执行失败的特定命令:

child=pexpect.spawn('ssh admin@%s'%ip_addr)

ip_addr is one of the arguments passed from PHP. ip_addr是从PHP传递的参数之一。 Right below this command I am ip_addr so as to find what value is passed to the command. 在此命令的正下方,我是ip_addr,以便查找将什么值传递给该命令。 and the value is as expected. 并且值是预期的。

I have the ip_addr variable stored correctly. 我有正确存储的ip_addr变量。 I have even type casted it to string so that should not be problem. 我什至已经将其强制转换为字符串,所以应该没有问题。

In general, is there a difference of format in passing variables from the command line and passing from PHP 通常,从命令行传递变量和从PHP传递变量的格式是否有所不同

For my understanding i think passing it through shell and php is the same, i can't be sure for 100% but that what i know from experience. 以我的理解,我认为通过shell和php传递它是相同的,我不能确定100%,但是我从经验中知道。

For your case if you have verified with print that it's the right string to pass (mean take the output and test it in the terminal if it work) if it work, than verify if the python script file is in the same directory as the php script file , because the shell will be opened at the current php script path . 对于您的情况,如果您已经用print验证了它是正确的传递字符串(意味着将输出通过并在终端中对其进行测试(如果它可以工作))是否有效, 那么请验证python脚本文件是否与php位于同一目录中脚本文件 ,因为该外壳将在当前的php脚本路径中打开 And so the shell command should respect that . 因此,shell命令应该尊重这一点 For example let assume php script file is in the root "/" , and python script file is in "/myPythonScripts/imageProccessing/" then when you execute something like "python myPyScript.py 646545645" for example, in this example in php you should execut "python ./myPythonScripts/imageProccessing/myPyScript.py 646545645" , as i said the shell is launched on the php script path. 例如,假设php脚本文件位于根目录“ /”中 ,而python脚本文件位于“ / myPythonScripts / imageProccessing /”中,那么当您执行“ python myPyScript.py 646545645”之类的示例时,在此示例中,您在php中应该执行“ python ./myPythonScripts/imageProccessing/myPyScript.py 646545645” ,正如我所说的,shell是在php脚本路径上启动的。

Now if that's not the problem, then try escaping the string first , with escapeshellcmd() . 现在,如果这不是问题,请尝试使用escapeshellcmd() 转义字符串 And see what it give. 看看它能给什么。 There is characters that are not supported by the shell, or that have a certain signification and parsed by the shell differently . 有些字符不被外壳支持,或者具有一定的含义并且被外壳不同地解析 make sure none of them is passed as argument. 确保它们都不作为参数传递。

In case none of the above is helpful try to use execshell() instead, and see what it give. 如果以上方法均无济于事,请尝试使用execshell()代替,并查看其作用。 Too probable that will not make a difference. 太有可能不会有所作为。

Edit: In case nothing helped, try to change just a bit the way of the exchange is done, and use the way it's done in this response using json https://stackoverflow.com/a/14048046/7668448 (it's a kind of more convenient) 编辑:如果没有任何帮助,请尝试稍微改变交换的方式,并使用json https://stackoverflow.com/a/14048046/7668448在此响应中使用完成的方式(这是一种更方便)

Please if any of that solved the problem, precise in a response which one was! 如果有任何解决问题的方法,请准确答复。 I hope it will help. 希望对您有所帮助。 Also if it's not your case it may be useful for other persons in the future. 另外,如果不是您的情况,将来对其他人可能也会有用。 Think also about giving the solution when you do! 当您这样做时,也要考虑提供解​​决方案! (i also personally interested too) (我个人也有兴趣)

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

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