PHP exec(python.py), 写入文件的权限被拒绝
PHP exec(python.py), permission denied writing file
我正在尝试使用 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 (most recent call last): File
"/var/www/cgi-bin/api/download.py", line 46, in
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 的 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 (most recent call last): File "/var/www/cgi-bin/api/download.py", line 46, in 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 个权限。
我知道这不是一个好的做法,但我想在做一些更清洁的事情之前解决这个问题。
提前致谢