返回Python CGI MySQL脚本的输出 [英] Returning Output of Python CGI MySQL Script

查看:87
本文介绍了返回Python CGI MySQL脚本的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和MySQL的新手,这是我的第一个Stack问题.因此,如果我遗漏了一些明显的内容,请提前道歉.但是,在问之前,我确实确实尝试过对此进行研究.

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.

我正在尝试学习Python,MySQL和CGI脚本的基础知识.为此,我一直在阅读 http://www.tutorialspoint.com/python/python_cgi_programming.htm http://www.tutorialspoint.com/python/python_database_access.htm 等.

I'm trying to learn the basics of Python, MySQL, and CGI scripting. 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.

我正在尝试让CURL GET或Python Requests GET在测试服务器上调用Python CGI脚本.然后,该Python CGI脚本将对本地MySQL数据库执行读取操作,并将结果返回到CURL或Python请求.

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.

我创建的Python CGI脚本将MySQL Read数据完美输出到远程测试服务器上的终端.但是,它不会将输出返回给CURL或触发CGI脚本的Python请求.

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.

这是我拼凑的Python CGI脚本:

This is the Python CGI script I've cobbled together:

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

我的猜测是,我需要通过某种Return或Output语句以某种方式将数据从SQL查询传递回Python或返回到请求过程.但是,我为研究此问题所做的所有最大努力都无济于事.谁能指出我正确的方向?非常感谢您的帮助!

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. 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):-)

Marc :-)

推荐答案

首先,根据您链接的CGI教程,您需要输出正在使用的内容类型:

First, as per the CGI tutorial you link, you need to output the content type you are working with:

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

如果您至少没有换行符,HTTP客户端会认为您的内容应该是标头的一部分,并且会感到困惑,他们可能会假设您要求的文档为空.

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.

接下来,您需要一个CGI服务器. Python附带了一个.将脚本放置在名为cgi-bin并运行此命令(从父目录):

Next, you need a CGI server. Python comes with one. Place your script in a subdirectory called cgi-bin and run this command (from the parent directory):

python -m CGIHTTPServer

要调用的网址如下所示:

The url to call will look something like this:

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

这篇关于返回Python CGI MySQL脚本的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆