【问题标题】:502 error on python pagepython页面上的502错误
【发布时间】:2014-07-22 23:52:20
【问题描述】:

我刚刚安装了 python 27,我正在尝试通过我的 localhost 服务器运行一个 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

【问题讨论】:

    标签: python iis-7


    【解决方案1】:

    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 响应,作为纯文本,因此浏览器将正确地向您显示数据。

    【讨论】:

    • 我添加了那行。现在我不再收到错误,但我也没有收到任何数据列表,所以我现在不确定问题是什么。
    • @LauraNMS 感谢您的反馈!我已经更新了代码以提供更多信息
    • 谢谢你,剃须猪。原来 iss 没有为 python 配置正确。我在这里得到了答案:stackoverflow.com/questions/6823316/…
    • 不客气@LauraNMS!如果你愿意,请接受这个答案。很高兴为您提供帮助。
    • 很遗憾,我从 stackoverflow.com/questions/6823316/ 得到了答案,但非常感谢您的回复!
    猜你喜欢
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多