http://www.tutorialspoint.com/python/python_cgi_programming.htm 'filename' 系统是debian squeeze, python 2.6.6, apache2 错误信息如下 Phase: 'PythonHandler' Handler: 'mod_python.publisher' Traceback (most recent call last): File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch default=default_handler, arg=req, silent=hlist.silent) File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1229, in _process_target result = _execute_target(config, req, object, arg) File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1128, in _execute_target result = object(arg) File "/usr/lib/python2.6/dist-packages/mod_python/publisher.py", line 204, in handler module = page_cache[req] File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1059, in __getitem__ return import_module(req.filename) File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 296, in import_module log, import_path) File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 680, in import_module execfile(file, module.__dict__) File "/home/ghuang/www/cgi/t1.py", line 9, in fileitem = form['filename'] File "/usr/lib/python2.6/cgi.py", line 541, in __getitem__ raise KeyError, key KeyError: 'filename' MODULE CACHE DETAILS Accessed: Fri Oct 7 00:25:55 2011 Generation: 17
action="save_file.py" method="post">
File:
Here is the script save_file.py to handle file upload: #!/usr/bin/python import cgi, os import cgitb; cgitb.enable() form = cgi.FieldStorage() # Get filename here. fileitem = form['filename'] # Test if the file was uploaded if fileitem.filename: # strip leading path from file name to avoid # directory traversal attacks fn = os.path.basename(fileitem.filename) open('/tmp/' + fn, 'wb').write(fileitem.file.read()) message = 'The file "' + fn + '" was uploaded successfully'