http 请求 cgi python 脚本成功,但该脚本似乎没有运行

http 请求 cgi python 脚本成功,但该脚本似乎没有运行

我已经为我的 apache2 网络服务器配置了 cgi 脚本。

这是我想要做的:

  1. 客户端将图像上传到服务器。(这已经起作用了)
  2. 成功后,我想执行 python 脚本来调整图像大小。

我尝试了以下操作,成功函数确实执行了,但我的 python 脚本似乎没有执行:

Javascript发送请求的代码:

var input = document.getElementById('imageLoader');
imageName = input.value;
var file = input.files[0];

if(file != undefined){
  formData= new FormData();
  console.log(formData.length);
  if(!!file.type.match(/image.*/)){
    formData.append("image", file);

    $.ajax({
      url: "upload.php",
      type: "POST",
      processData: false,
      contentType: false,
      success: function() {

        var input = document.getElementById('imageLoader');
        imageName = input.value;
        var file = input.files[0];
        formData = new FormData();
        formData.append("filename", file);

        $.ajax({
          url: "http://localhost/Main/cgi-bin/resize.py",
          type: "POST",
          data: formData,
          processData: false,
          contentType: false,
          success: function(data) {
            console.log(data);
          }
        });
        // code continues...

resize.py

#!/usr/bin/python

import cgi
import cgitb
import Image

cgitb.enable()

data = cgi.FieldStorage()

filename = data.getvalue("filename")
im = Image.open("../JS/upload/" + filename)

(width, height) = im.size

maxWidth = 600
maxHeight = 400

if width > maxWidth:
    d = float(width) / maxWidth
    height = int(height / d)
    width = maxWidth
if height > maxHeight:
    d = float(height) / maxHeight
    width = int(width / d)
    height = maxHeight

size = (width, height)
im = im.resize(size, Image.ANTIALIAS)

im.save("../JS/upload/" + filename, quality=100)

这是apache2.conf

<Directory /var/www/html/Main/cgi-bin>
    AllowOverride None
    Options +ExecCGI
    SetHandler cgi-script
    AddHandler cgi-script .py .cgi
    Order allow,deny
    Allow from all
</Directory>

cgi-bin和python脚本文件权限:

drwxrwxr-x  2 mou mou 4096 Aug 24 03:28 cgi-bin

-rwxrwxrwx 1 mou mou 1673 Aug 24 03:28 resize.py

编辑

执行此代码

$.ajax({
    url: "http://localhost/Main/cgi-bin/resize.py",
    type: "POST",
    data: formData,     // formData = {"filename" : "the filename which was saved in a variable whie the image was uploaded"}
    processData: false,
    contentType: false,
    success: function(data) {
        alert(data);
    }
});

它发出以下警报:

<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> -->
<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> -->
</font> </font> </font> </script> </object> </blockquote> </pre>
</table> </table> </table> </table> </table> </font> </font> </font><body bgcolor="#f0f0f8">
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#6622aa">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong>&lt;type 'exceptions.TypeError'&gt;</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial">Python 2.7.6: /usr/bin/python<br>Sun Aug 24 17:24:15 2014</font></td></tr></table>

<p>A problem occurred in a Python script.  Here is the sequence of
function calls leading up to the error, in the order they occurred.</p>
<table width="100%" cellspacing=0 cellpadding=0 border=0>
<tr><td bgcolor="#d8bbff"><big>&nbsp;</big><a href="file:///var/www/html/Main/cgi-bin/resize.py">/var/www/html/Main/cgi-bin/resize.py</a> in <strong><module></strong>()</td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;10</small>&nbsp;<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;11</small>&nbsp;filename&nbsp;=&nbsp;data.getvalue("filename")<br>
</tt></font></td></tr>
<tr><td bgcolor="#ffccee"><tt>=&gt;<small>&nbsp;&nbsp;&nbsp;12</small>&nbsp;im&nbsp;=&nbsp;Image.open("../JS/upload/"&nbsp;+&nbsp;filename)<br>
</tt></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;13</small>&nbsp;<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;14</small>&nbsp;(width,&nbsp;height)&nbsp;=&nbsp;im.size<br>
</tt></font></td></tr>
<tr><td><small><font color="#909090">im <em>undefined</em>, <strong>Image</strong>&nbsp;= &lt;module 'Image' from '/usr/lib/python2.7/dist-packages/PILcompat/Image.pyc'&gt;, Image.<strong>open</strong>&nbsp;= &lt;function open&gt;, <strong>filename</strong>&nbsp;= '<font color="#c040c0">\xff\xd8\xff\xe0\x00\x10</font>JFIF<font color="#c040c0">\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00</font>C<font color="#c040c0">\x00\x06\x04\x05\x06\x05\x04\x06\x06\x05\x06\x07\x07\x06\x08\n\x10\n\n\t\t\n\x14\x0e</font>...<font color="#c040c0">\x94\r\x17\x11</font>b<font color="#c040c0">\xcd\xdc\x1a\xfe\xf1\x05\x1b\x15\xd1</font>R<font color="#c040c0">\xce\xe9</font>*<font color="#c040c0">\xb5\x8e</font>b<font color="#c040c0">\x97\x82\x87</font>R<font color="#c040c0">\xf4\xaa</font>K<font color="#c040c0">\x83</font>6<font color="#c040c0">\xbf\xfb</font>0<font color="#c040c0">\xa0\xb6</font>8<font color="#c040c0">\xa9</font>C<font color="#c040c0">\x86\x8d\x96</font>n+E<font color="#c040c0">\xd3\x7f\x99\xff\xd9</font>'</font></small></td></tr></table>
<table width="100%" cellspacing=0 cellpadding=0 border=0>
<tr><td bgcolor="#d8bbff"><big>&nbsp;</big><a href="file:///usr/lib/python2.7/dist-packages/PIL/Image.py">/usr/lib/python2.7/dist-packages/PIL/Image.py</a> in <strong>open</strong>(fp='../JS/upload/<font color="#c040c0">\xff\xd8\xff\xe0\x00\x10</font>JFIF<font color="#c040c0">\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00</font>C<font color="#c040c0">\x00\x06\x04\x05\x06\x05\x04\x06\x06\x05\x06</font>...<font color="#c040c0">\x94\r\x17\x11</font>b<font color="#c040c0">\xcd\xdc\x1a\xfe\xf1\x05\x1b\x15\xd1</font>R<font color="#c040c0">\xce\xe9</font>*<font color="#c040c0">\xb5\x8e</font>b<font color="#c040c0">\x97\x82\x87</font>R<font color="#c040c0">\xf4\xaa</font>K<font color="#c040c0">\x83</font>6<font color="#c040c0">\xbf\xfb</font>0<font color="#c040c0">\xa0\xb6</font>8<font color="#c040c0">\xa9</font>C<font color="#c040c0">\x86\x8d\x96</font>n+E<font color="#c040c0">\xd3\x7f\x99\xff\xd9</font>', mode='r')</td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;1994</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;isPath(fp):<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;1995</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename&nbsp;=&nbsp;fp<br>
</tt></font></td></tr>
<tr><td bgcolor="#ffccee"><tt>=&gt;<small>&nbsp;1996</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fp&nbsp;=&nbsp;builtins.open(fp,&nbsp;"rb")<br>
</tt></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;1997</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;1998</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename&nbsp;=&nbsp;""<br>
</tt></font></td></tr>
<tr><td><small><font color="#909090"><strong>fp</strong>&nbsp;= '../JS/upload/<font color="#c040c0">\xff\xd8\xff\xe0\x00\x10</font>JFIF<font color="#c040c0">\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00</font>C<font color="#c040c0">\x00\x06\x04\x05\x06\x05\x04\x06\x06\x05\x06</font>...<font color="#c040c0">\x94\r\x17\x11</font>b<font color="#c040c0">\xcd\xdc\x1a\xfe\xf1\x05\x1b\x15\xd1</font>R<font color="#c040c0">\xce\xe9</font>*<font color="#c040c0">\xb5\x8e</font>b<font color="#c040c0">\x97\x82\x87</font>R<font color="#c040c0">\xf4\xaa</font>K<font color="#c040c0">\x83</font>6<font color="#c040c0">\xbf\xfb</font>0<font color="#c040c0">\xa0\xb6</font>8<font color="#c040c0">\xa9</font>C<font color="#c040c0">\x86\x8d\x96</font>n+E<font color="#c040c0">\xd3\x7f\x99\xff\xd9</font>', <em>global</em> <strong>builtins</strong>&nbsp;= &lt;module '__builtin__' (built-in)&gt;, builtins.<strong>open</strong>&nbsp;= &lt;built-in function open&gt;</font></small></td></tr></table><p><strong>&lt;type 'exceptions.TypeError'&gt;</strong>: file() argument 1 must be encoded string without NULL bytes, not str
<br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>args&nbsp;=
('file() argument 1 must be encoded string without NULL bytes, not str',)
<br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>message&nbsp;=
'file() argument 1 must be encoded string without NULL bytes, not str'


<!-- The above is a description of an error in a Python program, formatted
     for a Web browser because the 'cgitb' module was enabled.  In case you
     are not reading this in a Web browser, here is the original traceback:

Traceback (most recent call last):
  File "/var/www/html/Main/cgi-bin/resize.py", line 12, in &lt;module&gt;
    im = Image.open("../JS/upload/" + filename)
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1996, in open
    fp = builtins.open(fp, "rb")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str

-->

这是否意味着formData我发送过去的内容是空的?

答案1

我已经解决了这个问题,现在图片可以调整大小了。花了一整天时间找出问题,最终解决了,感觉真好。

问题出在这里:

formData.append('filename', file)    // file = { size: 1269115, type: "image/png", name: "desktop.png", path: "", lastModifiedDate: Date 2014-08-02T22:26:03.000Z, mozFullPath: "/home/mou/Pictures/desktop.png" }

在我的 cgi 中,python 脚本data.getvalue("filename")返回了整个内容。

因此,我将其更改filefile.nameFieldStorage(['filename', None, 'image.png'])

filename = data.getvalue("filename")现在获取我所替换的图像的名称filename = data['filename'].value并且它起作用了。

相关内容