简体   繁体   English

返回Python CGI MySQL脚本的输出

[英]Returning Output of Python CGI MySQL Script

I'm very new to Python and MySQL and this is my first Stack question. 我是Python和MySQL的新手,这是我的第一个Stack问题。 So, apologies in advance if I'm missing something obvious. 因此,如果我缺少明显的内容,请提前道歉。 But, I really did try to research this before asking. 但是,在询问之前,我确实确实尝试过对此进行研究。

I'm trying to learn the basics of Python, MySQL, and CGI scripting. 我正在尝试学习Python,MySQL和CGI脚本的基础知识。 To that end, I've been reading tutorials at http://www.tutorialspoint.com/python/python_cgi_programming.htm and http://www.tutorialspoint.com/python/python_database_access.htm , among others. 为此,我一直在阅读http://www.tutorialspoint.com/python/python_cgi_programming.htmhttp://www.tutorialspoint.com/python/python_database_access.htm上的教程。

I'm trying to have a CURL GET or Python Requests GET call a Python CGI script on a test server. 我正在尝试让CURL GET或Python Requests GET在测试服务器上调用Python CGI脚本。 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. 然后,该Python CGI脚本将在本地MySQL数据库上执行读取操作,并将结果返回到CURL或Python请求。

The Python CGI script I've created outputs the MySQL Read data perfectly to the terminal on the remote test server. 我创建的Python CGI脚本将MySQL Read数据完美输出到远程测试服务器上的终端。 But, it won't return that output to the CURL or to the Python Requests that triggered the CGI script. 但是,它不会将输出返回给CURL或触发CGI脚本的Python请求。

This is the Python CGI script I've cobbled together: 这是我拼凑的Python CGI脚本:

#!/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 > '%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()

My guess 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. 我的猜测是,我需要以某种方式将数据从SQL查询传递回Python或通过某种Return或Output语句传递回请求过程。 But, all my best efforts to research this have not helped. 但是,我为研究此问题所做的所有最大努力都无济于事。 Can anyone point me in the right direction? 谁能指出我正确的方向? Many thanks for any help! 非常感谢您的帮助!

Marc :-) 马克:-)

First, as per the CGI tutorial you link, you need to output the content type you are working with: 首先,按照您链接的CGI教程,您需要输出正在使用的内容类型:

print "Content-type: text/plain\r\n\r\n",

If you don't at least have the newlines in there, HTTP clients will think your content is supposed to be part of the headers and get confused, they will probably assume the document you asked for is empty. 如果您至少没有换行符,HTTP客户端会认为您的内容应该是标头的一部分并感到困惑,他们可能会假设您要的文档为空。

Next, you need a CGI server. 接下来,您需要一个CGI服务器。 Python comes with one. Python附带一个。 Place your script in a subdirectory called cgi-bin and run this command (from the parent directory): 将您的脚本放在一个名为cgi-bin的子目录中,然后运行此命令(从父目录):

python -m CGIHTTPServer

The url to call will look something like this: 调用的网址如下所示:

curl http://localhost:8000/cgi-bin/cgitest.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM