2014-05-14 27 views
1

我有腳本,它運行在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()') 

我怎樣才能得到這個函數的結果到同一網頁?

鏈接到如何的將是完美的。或者一些例子。

回答

1

您可以通過結合你的兩個片段做這樣的:

#!/usr/bin/env python 


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() 

print "Content-Type: text/html" 
print 
print """ 
<html> 
<body> 
<h2>Test</h2> 
""" 

myver('localhost', 'username', 'megapass', 'SELECT VERSION()') 

print """ 
</body> 
</html> 
""" 

如果你想做到這一點的一個按鈕的點擊,您將需要調查AJAX和東西有點比CGI更靈活如Flask

2

我認爲你可以用jquery和Flask做得更好。

Flask是一個非常容易使用的Python微框架,jQuery是一個JavaScript庫,它使Ajax變得輕而易舉。

一些代碼

from flask import Flask, request 

app = Flask(__name__) 

@app.route('/') 
def main(): 
    return """<html> 
        <head> 
         <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
         <script> 
          $(document).ready(function(){ 

           $('#btnSend').click(function(){ 

            $.ajax({ 
             type: 'POST', 
             url: '/process', 
             success: function(data){ 
             alert(data); 
             } 
            }); 
           }); 

          }); 
         </script> 
        </head> 
        <body> 
        <input type="button" id="btnSend" value="process"> 
        </body> 
        </html>""" 


@app.route('/process', methods=['POST']) 
def view_do_something(): 

    if request.method == 'POST': 
     #your database process here 
     return "OK" 
    else: 
     return "NO OK" 

if __name__ == '__main__': 
    app.run() 

嘗試以HTTP://本地主機:5000在瀏覽器中。

+0

謝謝,但這對我來說太複雜了:-) – setevoy

+2

如果您想做比您在問題中發佈的單頁應用程序更復雜的任何內容,Flask和jQuery將使你的生活比試圖通過CGI方式唾棄更容易一百萬倍。 – robbrit

+1

不客氣,但請聽@robbrit建議。 Jquery和Flask(或任何其他Python Web框架)都是這樣。 – andmart

0

找到一個解決方案 - 使用GET:如果有什麼錯

$ cat cgi-bin/mysql.py 
#!/usr/bin/python 

import cgi, MySQLdb 

data = None 
form = cgi.FieldStorage() 

def myver(host, user, pwd, cmd): 
    db = MySQLdb.connect(host, user, pwd) 
    cursor = db.cursor() 
    cursor.execute(cmd) 
    global data 
    data = cursor.fetchone() 
    db.close() 

form = cgi.FieldStorage() 

print "Content-type:text/html\r\n\r\n" 
print "<title>Test to get MySQL version</title>" 
print "<h2>MySQL version</h2>" 
print '''Get version: <form action="/cgi-bin/mysql.py" method="get"> 
<input type="submit" name="getvers" value="Get version" /> 
<input type="submit" name="exit" value="Exit" /> 
</form> 
''' 

if "getvers" in form: 
    myver('localhost', 'username', 'megapass', 'SELECT VERSION()') 
    print 'Current MySQL version: ' + ''.join(data) 
elif "exit" in form: 
    print 'Exit' 

請指正......但是 - 它的工作原理。

P.S.無法運行POST方法:-(