我正在尝试使用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
如果将.py或.php作为Apache用户执行,则一切正常。 但是在编写文件时使用我的浏览器:
fh = io.FileIO("api/test.pdf","wb")
我在apache error_log中输出了这个输出:
Traceback(最近一次调用最后一次):文件 “/var/www/cgi-bin/api/download.py”,第46行,in fh = io.FileIO(“test.pdf”,“wb”)IOError:[Errno 13]权限被拒绝:'test.pdf'
好吧,我很确定这是由于父文件夹的权限,然后是父级父级的权限。但我为这些文件夹设置了777权限,但它没有做任何事情。
我的php脚本位于/var/www/html/manager/execute.php中 我的.py脚本位于/var/www/cgi-bin/api/download.py
中folders / www /,/ cgi-bin /,/ api /拥有777项权利。
我知道这不是一个好习惯,但我想在做更清洁的事情之前解决问题。
提前致谢
答案 0 :(得分:0)