显示结果 python 命令到 web cgi

Showing results python command to the web cgi

我有一个 python 脚本,如果在终端或命令行中执行,它 运行 很好,但在我尝试之后甚至发生了内部服务器错误。如何在网络上增强脚本 运行。

HTML

<html><body>
<form enctype="multipart/form-data" action="http://localhost/cgi-bin/coba5.py" method="post">
<p>File: <input type="file" name="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body></html>

PYTHON

#!/usr/bin/python
import cgi, os
import cgitb; cgitb.enable()
import simplejson as json
import optparse
import sys

try: # Windows needs stdio set for binary mode.
    import msvcrt
    msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
    msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
    pass

form = cgi.FieldStorage()

# A nested FieldStorage instance holds the file
fileitem = form['file']
fn = os.path.basename(fileitem.filename)
data = open('/tmp/upload-cgi/' + fn, 'wb').write(fileitem.file.read())
data=fileitem.file.read()
location_database = open('/home/bioinformatics2/DW/taxo.json', 'r')
database = json.load(location_database)

for line in data: 
 for taxonomy in database:  
  if taxonomy["genus"] == line.replace('\n','') :
   print "Kingdom: %s," % taxonomy['kingdom'],
   print "phylum: %s," % taxonomy['phylum'],
   print "class: %s," % taxonomy['class'],
   print "order: %s," % taxonomy['order'],
   print "family: %s," % taxonomy['family'],
   print "genus: %s" % taxonomy['genus']
   break
 else:
  print ("No found genus on taxanomy")

您没有输出 HTTP header。至少,你需要输出:

print "Content-Type: text/plain"
print ""

您也正在阅读该文件两次:

data = open('/tmp/upload-cgi/' + fn, 'wb').write(fileitem.file.read())
data=fileitem.file.read()

第二个 fileitem.file.read() 可能不会读取任何内容,因为文件应该已经在 end-of-file,因此 data 将为空。