2009-08-21 24 views
0

我已經創建了一個帶有表單的HTML頁面,它接受一個名稱和密碼,並將它傳遞給一個Python腳本,該腳本應該用歡迎信息打印人員名稱。但是,在POST值後,我只是得到了瀏覽器中顯示的Python代碼,而不是歡迎消息。我已將html文件和python文件存儲在Apache 2.2下的cgi-bin文件夾中。如果我只是在瀏覽器中運行一個簡單的hello world python腳本,就會顯示「Hello World」消息。我使用WinXP,Python 2.5,Apache 2.2。我想要運行的代碼如下:HTML表單不能與python合作

#!c:\python25\python.exe 
import cgi 
import cgitb; cgitb.enable() 
form = cgi.FieldStorage() 
reshtml = """Content-Type: text/html\n 
<html> 
<head><title>Security Precaution</title></head> 
<body> 
""" 

print reshtml 

User = form['UserName'].value 
Pass = form['PassWord'].value 

if User == 'Gold' and Pass == 'finger': 
    print '<big><big>Welcome' 
    print 'mr. Goldfinger !</big></big><br>' 
    print '<br>' 
else: 
    print 'Sorry, incorrect user name or password' 
    print '</body>' 
print '</html>' 

這個問題的答案可能是非常明顯的,但它完全逃避我。我對Python很陌生,所以任何幫助將不勝感激。

謝謝。

+1

爲什麼不使用mod_wsgi或正確的web框架? – 2009-08-21 10:37:41

回答

1

我剛開始在瀏覽器

聽起來像CGI與Apache和Python處理顯示的Python代碼 配置不正確。

可以拉近通過將用戶名和密碼作爲GET參數的測試案例:

http://example.com/cgi-bin/my-script.py?UserName=Foo&PassWord=bar 

會發生什麼事,如果你做到這一點?

0

您可能需要提取這樣

User = form.getfirst('UserName') 
Pass = form.getfirst('PassWord') 

我知道的字段值,這是奇怪的。