Python在php中运行时给出错误,但在终端中未显示

时间:2019-12-19 21:37:12

标签: php python apache raspbian geckodriver

(Raspbian Buster,Apache2,PHP 7,Python 3,Geckodriver 0.23)

我有一个Python脚本,该脚本由具有shell_exec()的php脚本调用。当我在与php脚本相同的位置从终端运行python脚本时,它的工作原理很吸引人。但是,当我使用本地主机访问我的apache服务器时,php可以工作,但是python脚本无法运行。

我做了一些研究,发现php错误日志也显示了我的python错误。它说我需要将我的geckodriver放在PATH中(见下文),当我在终端中运行它时,它也可以工作,但是突然从php中运行时,它就不需要了。不能有任何许可问题,我将所有所有者的权限授予了www-data组。

错误:

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

PHP代码:

if ($_SESSION["loaded"] == False) {
    $output = shell_exec('python3 py/infoam.py');
    $_SESSION["loaded"] = True;
} elseif ($_SESSION["agendanr"] == 0 and $_SESSION["magisternr"] == 0) {
    $output = shell_exec('python3 py/infoam.py');
} elseif ($_SESSION["agendanr"] == 1 and $_SESSION["magisternr"] == 0) {
    $output = shell_exec('python3 py/infona.py');
} elseif ($_SESSION["agendanr"] == 0 and $_SESSION["magisternr"] == 1) {
    $output = shell_exec('python3 py/infonm.py');
} else {
    $output = shell_exec('python3 py/infonam.py');
}

相关的python代码:

from __future__ import print_function
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import requests
import time
import pickle
import os.path
headers = {"User-agent":"*my user agent*"}

class client:
    def __init__(self, usr, pwd):
        self.usr = usr
        self.pwd = pwd

    def magister(self):
        driver = webdriver.Firefox()
        driver.get("https://farel.magister.net/magister/#/vandaag")
        driver.maximize_window()
        driver.implicitly_wait(20)
        time.sleep(5)
        elem = driver.find_element_by_id("username")
        elem.send_keys(self.usr)
        elem.send_keys(u'\ue007')
        driver.maximize_window()
        driver.implicitly_wait(20)
        elem = driver.find_element_by_id("rswp_password")
        elem.send_keys(self.pwd)
        elem.send_keys(u'\ue007')
        driver.maximize_window()
        driver.implicitly_wait(10)
        time.sleep(5)
        elem = driver.find_element_by_class_name("last-grade").text
        lis = elem.split()
        grade, clas = lis[0], lis[1]
        driver.quit()
        return(grade + '\n' + clas + '\n')

    def write(self):
        file = '/var/www/html/txt/' + self.usr + '.txt'

    with open(file, 'w') as writefile:
            writefile.write(self.magister())

def read():
    f = open("/var/www/html/txt/acc.txt", "r")
    for x in f:
        keys = []
        for y in x.split(','):
            keys.append(y.strip())
        print(keys)
        person = client(keys[0], keys[1])
        person.write()

read()

有人知道为什么会引发此错误吗?

0 个答案:

没有答案