简体   繁体   English

通过php执行python脚本

[英]Executing a python script via php

So Im trying to execute a python script from php using this code 所以我试图使用此代码从php执行python脚本

exec('C:\Python27\python.exe    C:\WEBSITE_DATA\script.py');

or with double backspace... 或带有双退格键...

exec('C:\\Python27\\python.exe    C:\\WEBSITE_DATA\\script.py');

the script is functional and generates a shape file when ran normally so the issue is not with the .py file. 该脚本可以正常运行,并在正常运行时生成一个形状文件,因此问题不在于.py文件。

I changed permissions to allow everyone full control from the python27 folder and the website data folder just to check if it were permissions issue 我更改了权限,允许所有人从python27文件夹和网站数据文件夹中完全控制,只是检查它是否是权限问题

php log is absent of any errors so I can't chase don't any bugs or problems with code php日志中没有任何错误,因此我无法追查没有任何错误或代码问题

I'm running IIS on windows server 2012 r2 is this my issue? 我在Windows Server 2012 R2上运行IIS,这是我的问题吗? Is what I'm trying to do even possible? 我要做什么甚至有可能吗?

also if its possible can I exchange variables from php to python and then back? 还可能的话,我可以将变量从php交换到python,然后再返回吗?

That is very bad thing to do. 那是非常不好的事情。 That is very hacky and unsafe solution. 那是非常hacky和不安全的解决方案。 Keep this in mind. 请记住这一点。

With this said, you should be looking for php system function that executes the command in console, not exec . 如此说来,您应该寻找在控制台而不是exec中执行命令的php system功能。

There are multiple ways to transfer variables from php script to Python. 有多种方法可以将变量从php脚本传输到Python。 You can write data to MySQL database and then read them from Python script. 您可以将数据写入MySQL数据库,然后从Python脚本读取它们。 You can pack all the variables in JSON using php zip and json_encode functions, save the JSON to file on disk, and then read it from the same location in Python script. 您可以使用php zip和json_encode函数将所有变量打包到JSON中,将JSON保存到磁盘上的文件中,然后从Python脚本中的同一位置读取它。

The simplest way though is to do this in your system call: 不过,最简单的方法是在system调用中执行此操作:

system("C:\Python27\python.exe /path/to/script.py $a $b $c")

This way the variables will be the arguments to Python script. 这样,变量将成为Python脚本的参数。 There you ca just read them from sys.argv array like that: 在那里,您可以像这样从sys.argv数组中读取它们:

import sys
a = sys.argv[1] # Note that the first argument is [1], not [0]
b = sys.argv[2]
...

To transfer values back to PHP script I suggest the same thing - saving them to JSON on the disk and then reading and parsing this JSON in PHP script after the execution of Python script is finished. 要将值传输回PHP脚本,我建议做同样的事情-将它们保存到磁盘上的JSON,然后在完成Python脚本执行后在PHP脚本中读取和解析此JSON。

Now, although I told you how to do that, you absolutely should not. 现在,尽管我告诉过您如何做,但您绝对不应该这样做。 Much better way is to write a CGI script on Python that will process the request and do the work you initially written in Python.You can read more here - https://www.linux.com/blog/configuring-apache2-run-python-scripts 更好的办法是写在Python中的CGI脚本,将处理请求和你最初写在Python.You工作可以在这里阅读更多- https://www.linux.com/blog/configuring-apache2-run- python脚本

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

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