Re: [問題] cgi上面 import MySQLdb 無法成功,但갠…
※ 引述《magicallove (小判)》之銘言:
: 我寫了下面這隻python程式要來獲得MySQL裡面的資料
: #!/usr/bin/python
: #coding:utf-8
: import MySQLdb
: conn = MySQLdb.connect(host="x.x.x.x",user="xx",passwd="xx",db="xx")
: cursor = conn.cursor()
: cursor.execute("select * from rule")
: result = cursor.fetchall()
: print "Content-Type: text/html" # HTML is following
: print # blank line, end of headers
: print "<html>"
: print "<head>"
: print "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"
: print "<title> Python import MySQLdb</title>"
: print "</head>"
: print "<body>"
: for record in result:
: print record[0]
: print "</body>"
: 如果我直接在 console 裡面 python sql.py的話可以正確獲得資料
: 但是放到網站上面想要作成cgi來用的時候就會出現
: Internal Server Error
: The server encountered an internal error or misconfiguration and was unable
: to complete your request.
程式的 output 沒有符合 HTTP protocol。
print "HTTP/1.0 200 OK"
print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print "<html>"
print "<head>"
...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.173.139.26
Python 近期熱門文章
PTT數位生活區 即時熱門文章