2014-07-21 55 views
0

我剛剛安裝了Python 27,我試圖通過我的本地主機服務器運行一個python頁面。我收到此錯誤:python頁面上的502錯誤

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 

回答

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響應,如純文本,這樣瀏覽器會正確顯示您的數據。

+0

我添加了該行。現在我不再收到錯誤,但我也沒有收到任何數據清單,所以我現在不確定問題是什麼。 – LauraNMS

+0

@LauraNMS感謝您的反饋!我已更新代碼以提供更多信息 – johntellsall

+0

謝謝,shavenwarthog。原來iss沒有爲python配置。我在這裏得到了答案:http://stackoverflow.com/questions/6823316/python-on-iis-how?s=554506ed-4da3-4f4b-a548-0944c9bad077 – LauraNMS