简体   繁体   English

如何从PHP执行脚本

[英]How to execute a script from PHP

I have a PHP script that should run a Python script, what i try. 我有一个应该运行Python脚本的PHP脚本,我尝试了什么。 Nothing seems to work. 似乎没有任何作用。 I gived the script and the PHP script full permission (777). 我授予了脚本和PHP脚本完全权限(777)。 The way that i use to execute my Python script in php is: 我用来在php中执行Python脚本的方式是:

    <?php 


    shell_exec('python /var/www/html/send.py');
    system('python /var/www/html/send.py');

    ?>

I tried a couple of methodes that are in the code above, but nothing seems to solve the issue. 我尝试了上面代码中的几种方法,但是似乎没有任何方法可以解决该问题。 The strange thing is that the command is working fine when i execute it in Putty. 奇怪的是,当我在Putty中执行命令时,该命令运行正常。 So i don't know what could be the problem. 所以我不知道可能是什么问题。 The permissions must be right. 权限必须正确。

I found a few ways on Stack Overflow, but nothing has the same issue as me. 我发现了一些关于Stack Overflow的方法,但是没有一个问题和我一样。 This is the code that should be executed: 这是应该执行的代码:

#!/usr/bin/env python

import sys                              #import sys
import serial                           #import Serial
import time

ser = serial.Serial ("/dev/ttyS0")      #Open onze poort
ser.baudrate = 9600                     #Baudrate instellen

adres = 2
data = 1

print adres                             #Debug Info
print data                              #Debug Info

byte = chr(int(adres))                  # Omzetten adres naar Byte
ser.write(byte)                         # Uitsturen van adres over Serieeel
time.sleep(0.5)                         # Wachten (Atmel kan sloom zijn)
byte2 = chr(int(data))                  # Omzetten data naar Byte
ser.write(byte2)                        # Uitsturen van data over Serieeel

ser.close()                             # Seriele poort sluiten

Be sure that you have enabled exec in php.ini and just put into php code this 确保您已在php.ini中启用了exec并将其放入php代码中

<?php 
exec('python /var/www/html/send.py');

I think, you should run python from php's tree. 我认为,您应该从php的树中运行python。 For example: 例如:

php script
/var/www/run.php

python script:
/var/www/python/send.py

after that everything should be ok 之后一切都应该没事

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

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