从php错误执行python文件

时间:2014-10-10 16:28:07

标签: php python django shell

我有一些问题。

这个php(uploads.php)脚本执行gif.py python文件。

<?php


    $file_path = "uploads/";
    $auth_token = $_POST['auth_token'];
    mysql_connect('localhost',"login",'pass');
    mysql_set_charset("UTF-8");
    mysql_select_db("DB_name");

    $sql = "SELECT user_id FROM users WHERE auth_token='$auth_token'";
    $result = mysql_query($sql);

    if(mysql_fetch_array($result)['user_id']==0)
    {
        echo "fail auth_token ".$auth_token;
        exit;
    }

    $file_md5 = md5(time()+$auth_token+rand(0,1000));

    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], "uploads/".$file_md5.".mp4")) {
        $aaa = exec("/usr/bin/python gif.py b9022c5c317cf2317ad3f537dcc1cfbe");
        echo $file_md5."   ".$aaa;
    } else{
        echo "fail ".$_FILES['uploaded_file']['size']." ".$_FILES['uploaded_file']['tmp_name']." ".$file_path ;
    }
 ?>

gif.py文件

#!/usr/bin/env python
from moviepy.editor import *
import sys

file_md5 = sys.argv[1]

anna_olaf = (VideoFileClip("uploads/" + file_md5+".mp4")
     .subclip(0,1)
         .resize(1))  

anna_olaf.write_gif('gifs/test.gif', fps=15)

我有上传文件夹b9022c5c317cf2317ad3f537dcc1cfbe.mp4文件。我从Linux控制台调用gif.py或uploads.php文件都ok(php uploads.php或pyhon gif.py),但我从浏览器python文件调用uploads.php不起作用,在apache日志中我发现

/usr/lib/python2.7/dist-packages/IPython/utils/path.py:296: UserWarning: IPython parent '/var/www' is not a writable location, using a temp directory.
  " using a temp directory."%parent)
Traceback (most recent call last):
  File "gif.py", line 2, in <module>
    from moviepy.editor import *
  File "/usr/local/lib/python2.7/dist-packages/moviepy-0.2.1.8.12-py2.7.egg/moviepy/editor.py", line 45, in <module>
    from .video.io.sliders import sliders
  File "/usr/local/lib/python2.7/dist-packages/moviepy-0.2.1.8.12-py2.7.egg/moviepy/video/io/sliders.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 774, in <module>
    rcParams = rc_params()
  File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 692, in rc_params
    fname = matplotlib_fname()
  File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 604, in matplotlib_fname
    fname = os.path.join(get_configdir(), 'matplotlibrc')
  File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 253, in wrapper
    ret = func(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 478, in _get_configdir
    raise RuntimeError("Failed to create %s/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"%h)
RuntimeError: Failed to create /var/www/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data

请帮忙,在2行中的日志写入错误(来自moviepy.editor import *)但为什么我没有错误调用哪个(python gif.py)?

1 个答案:

答案 0 :(得分:1)

如评论中所述,问题显示在错误报告的最后一行,请参阅:

RuntimeError: Failed to create /var/www/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data 

我想说这可能是/var/www/文件夹的权限问题,不允许脚本根据需要创建文件。

要解决此问题,您应该更改文件夹的权限以使其可写,或者指定脚本应该为其创建所需文件的其他目录。