Showing results python command to the web cgi
<p>I have a python script and it runs well if executed in a terminal or command line , but after I try even internal server error occurred . how to enhance the script to be run on the web.</p>

<p>HTML</p>

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

<p>PYTHON</p>

<pre><code>#!/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")
</code></pre>

<p>You're not outputting the HTTP header.  At a minimum, you need to output:</p>

<pre><code>print "Content-Type: text/plain"
print ""
</code></pre>

<p>You are also reading the file twice:</p>

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

<p>The second <code>fileitem.file.read()</code> will probably not read any content, since the file should already be at the end-of-file, and so <code>data</code> will be empty.</p>

python cgi cgi-bin