title: Returning Output of Python CGI MySQL Script
body: <p>I'm very new to Python and MySQL and this is my first Stack question. So, apologies in advance if I'm missing something obvious. But, I really did try to research this before asking.</p>
  
  <p>I'm trying to learn the basics of Python, MySQL, and CGI scripting. To that end, I've been reading tutorials at <a href="http://www.tutorialspoint.com/python/python_cgi_programming.htm" rel="nofollow">http://www.tutorialspoint.com/python/python_cgi_programming.htm</a> and <a href="http://www.tutorialspoint.com/python/python_database_access.htm" rel="nofollow">http://www.tutorialspoint.com/python/python_database_access.htm</a>, among others.</p>
  
  <p>I'm trying to have a CURL GET or Python Requests GET call a Python CGI script on a test server. That Python CGI script would then perform a Read action on a local MySQL database and return the results to CURL or to Python Requests.</p>
  
  <p>The Python CGI script I've created outputs the MySQL Read data perfectly to the terminal on the remote test server. But, it won't return that output to the CURL or to the Python Requests that triggered the CGI script.</p>
  
  <p>This is the Python CGI script I've cobbled together:</p>
  
  <pre><code>#!/usr/bin/python
  
  import MySQLdb
  
  # Open database connection
  db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
  
  # prepare a cursor object using cursor() method
  cursor = db.cursor()
  
  # Prepare SQL query to INSERT a record into the database.
  sql = "SELECT * FROM EMPLOYEE \
         WHERE INCOME &gt; '%d'" % (1000)
  try:
     # Execute the SQL command
     cursor.execute(sql)
     # Fetch all the rows in a list of lists.
     results = cursor.fetchall()
     for row in results:
        fname = row[0]
        lname = row[1]
        age = row[2]
        sex = row[3]
        income = row[4]
        # Now print fetched result
        print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
               (fname, lname, age, sex, income )
  except:
     print "Error: unable to fetch data"
  
  # disconnect from server
  db.close()
  </code></pre>
  
  <p>My <em>guess</em> is that I need to somehow pass the data from the SQL query back to Python or back to the requesting process through some sort of Return or Output statement. But, all my best efforts to research this have not helped. Can anyone point me in the right direction? Many thanks for any help!</p>
  
  <p>Marc  :-)</p>