Python页面出现502错误

0 投票
1 回答
2741 浏览
提问于 2025-04-18 14:09

我刚安装了Python 2.7,想通过我的本地服务器运行一个Python页面。结果出现了这个错误:

HTTP错误502.2 - 错误的网关 指定的CGI应用程序没有正常工作,没有返回完整的HTTP头信息。它返回的头信息是""。

import MySQLdb

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                     user="root", # your username
                      passwd="pass", # your password
                      db="my_dbs") # name of the data base

cur = db.cursor() 

cur.execute("SELECT * FROM myTable")

# print all the first cell of all the rows
for row in cur.fetchall() :
    print(row[0])

我在使用Windows 7。

你知道我哪里出错了吗?这是不是IIS配置的问题?我有一个Python处理程序映射:

pythonHandler
Path: *.py
State: Enabled
Path Type: Unspecified
Handler: CGIModule
Entry Type: Local

1 个回答

0

IIS(互联网信息服务)在等待一个HTTP响应,但你却给它返回了一堆原始数据。

下面的代码会输出一个简单的文本网页。它会显示找到的行数,以及每行数据的第一列内容。

import MySQLdb

print 'Content-type: text/plain'
print

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                     user="root", # your username
                      passwd="pass", # your password
                      db="my_dbs") # name of the data base

cur = db.cursor() 

cur.execute("SELECT * FROM myTable")

rows = cur.fetchall()
print "found {} rows".format(len(rows))

# print all the first cell of all the rows
for row in rows:
    print(row[0])

这个最简单的头部信息把数据标记为HTTP响应,并且是纯文本格式,这样浏览器才能正确地显示这些数据。

撰写回答