【问题标题】:PHP exec(python.py), permission denied writing filePHP exec(python.py),权限被拒绝写入文件
【发布时间】:2017-07-11 02:30:54
【问题描述】:

我正在尝试使用 PHP 的 exec() 函数执行 python 脚本。

.php 页面通过浏览器加载:

<?php
 echo exec("whoami");

echo(exec("/bin/python2.7 /var/www/cgi-bin/api/download.py"));
?>

这里是python脚本:

# -*- encoding: UTF-8 -*-

import os
import httplib2
import io
import sys
from httplib2 import Http
from oauth2client import client
from oauth2client import tools
from apiclient import discovery
from oauth2client.file import Storage
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
from apiclient.http import MediaIoBaseDownload
from oauth2client.service_account import ServiceAccountCredentials

scopes = ['https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('/opt/scripts/full-patator-preprod.json', scopes)

delegated_credentials = credentials.create_delegated('#############')
http_auth = delegated_credentials.authorize(Http())

drive_service = build('drive', 'v2', http=http_auth)


def get_file(service,fileId):
    file = service.files().get(fileId=fileId).execute(http=http_auth);
    return file




item = get_file(drive_service,"#############")

print(item.get('title'))
delegated_credentials = credentials.create_delegated("#############")
http_auth = delegated_credentials.authorize(Http())


request = drive_service.files().export_media(fileId="#############", mimeType='application/pdf')

fh = io.FileIO("api/test.pdf","wb")
downloader = MediaIoBaseDownload(fh, request)
done = False

counter=0;
while done is False:
    status, done = downloader.next_chunk()
    print "Download %d%%." % int(status.progress() * 100)
    counter+=1
    if counter > 10:
        done=True
with open("test.pdf") as f: ## PDF File
    print(type(f))           ## Open file is TextIOWrapper
    bw=io.TextIOWrapper(fh)   ## Conversion to TextIOWrapper
    print(type(bw))          ## Just to confirm

如果以 Apache 用户身份执行 .py 或 .php,一切正常。 但是使用我的浏览器,在写入文件时:

fh = io.FileIO("api/test.pdf","wb")

我的 apache error_log 中有这个输出:

Traceback(最近一次调用最后一次):文件 “/var/www/cgi-bin/api/download.py”,第 46 行,在 fh = io.FileIO("test.pdf","wb") IOError: [Errno 13] Permission denied: 'test.pdf'

嗯,我很确定这是由于父文件夹的权限,然后是父文件夹的权限。但是我为这些文件夹设置了 777 权限,但它什么也没做。

我的 php 脚本在 /var/www/html/manager/execute.php 我的 .py 脚本位于 /var/www/cgi-bin/api/download.py

文件夹/www/、/cgi-bin/、/api/有777个权限。

我知道这不是一个好习惯,但我想在做一些更清洁的事情之前解决这个问题。

提前致谢

【问题讨论】:

  • 如果您通过浏览器调用 php 文件,您将成为用户 www-data。通常www-data 在命令行上拥有/不应该拥有太多权限。
  • PHP 脚本以谁的身份运行?该用户/组是否有权运行 Python 脚本?
  • 您好,我可以假设 php 以 apache 用户身份运行,因为当我执行时:shell_exec("whoami");它返回我“apache”当然,apache 是 .py 文件的所有者
  • 获取文件和打开文件方法,我很困惑因为哪个服务器允许更改打开方法,得到了很大的组合。从源代码读取块但如何声明系统和网络限制?将文件下载到 ram 并保留它。读取源的权限与目标文件的权限不同。
  • dsgdfg,get file 方法在上面声明,并使用 Google Python SDK 从 Google Drive 获取文件。正如我所说,脚本在 cli 中以 root 或 apache 运行时运行良好。但是在使用浏览器调用 .php 时被阻止。唯一的目标是 test.pdf

标签: php python apache centos7


【解决方案1】:

【讨论】:

  • 你好克里斯托弗,我刚刚尝试过,现在,我在输出中没有任何权限错误,但没有任何反应......而且我的脚本超时
猜你喜欢
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多