我有腳本,它運行在CGIHTTPServer
下。HTML按鈕來運行Python函數
腳本包含:
$ cat cgi-bin/mysql.py
#!/usr/bin/env python
print "Content-Type: text/html"
print
print """
<html>
<body>
<h2>Test</h2>
<button type="button">Get version</button>
</body>
</html>
"""
和功能(在其他腳本或此):
def myver(host, user, pwd, cmd):
db = MySQLdb.connect(host, user, pwd)
cursor = db.cursor()
cursor.execute(cmd)
data = cursor.fetchone()
print "Database version : %s " % data
db.close()
myver('localhost', 'username', 'megapass', 'SELECT VERSION()')
我怎樣才能得到這個函數的結果到同一網頁?
鏈接到如何的將是完美的。或者一些例子。
謝謝,但這對我來說太複雜了:-) – setevoy
如果您想做比您在問題中發佈的單頁應用程序更復雜的任何內容,Flask和jQuery將使你的生活比試圖通過CGI方式唾棄更容易一百萬倍。 – robbrit
不客氣,但請聽@robbrit建議。 Jquery和Flask(或任何其他Python Web框架)都是這樣。 – andmart