KeyError:使用karrigell链接python和html时的'filename'

时间:2014-10-03 15:32:20

标签: html python-2.7 cgi

我正在尝试使用python 2.7和Karrigell读取上传的文件。但它告诉我:

Traceback (most recent call last):
  File "C:\Karrigell-3.1.1\karrigell\core\HTTP.py", line 333, in process_request
  File "C:\Karrigell-3.1.1\karrigell\core\k_target.py", line 399, in run
  File "", line 7, in 
  File "cgi.pyc", line 541, in __getitem__
KeyError: 'filename'

这是我的web_access.py:

import cgi, os

form = cgi.FieldStorage()
fileitem=form['filename']
if fileitem.file:
    print fileitem.file.read()

else:
    print "Error"

我的Html页面:

<section>
    Enter the text: 
    <form action="web_access.py" method="post" enctype="multipart/form-data">
        <input type="file" name="filename" id="file" value="" size="50"/>
        <br />
        <input type="submit" value="Analyze"/>
    </form>
</section>

请帮帮我!

1 个答案:

答案 0 :(得分:0)

对于Karrigell,您不需要使用CGI模块;您将表单字段的引用作为内置对象REQUEST

的键

web_access.py可能类似于

print REQUEST['filename'].file.read()

记录在案here