【问题标题】:Showing results python command to the web cgi向网络 cgi 显示结果 python 命令
【发布时间】:2016-09-09 02:54:27
【问题描述】:

我有一个 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")

【问题讨论】:

    标签: python cgi cgi-bin


    【解决方案1】:

    您没有输出 HTTP 标头。至少,您需要输出:

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

    你也在读取文件两次:

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

    第二个fileitem.file.read() 可能不会读取任何内容,因为文件应该已经在文件末尾,所以data 将是空的。

    【讨论】:

    • 我尝试在print "Kingdom: %s," % taxonomy['kingdom'],上方添加print "Content-Type: text/plain",但仍然是同样的错误
    • 您需要这两行 - Content-Type 行和空白行。
    • 请编辑问题以包含在命令行上运行脚本的输出,如果您可以访问它,则包含来自 Web 服务器错误日志的错误输出
    • 在命令行上运行脚本的输出是空白的?
    • 来自网络的空白结果,如果从命令行工作并生成输出值
    猜你喜欢
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多