[英]CGI script with Python - Testing browser on local server
我正在努力嘗試在Windows機器上本地測試CGI Python腳本。
我按照教程,所以它很簡單。 這是我的Python腳本(名為test_cgi.py,位於cgi-bin目錄中)。
print "Content-type: text/html"
print "<html>"
print "<title>Test CGI</title>"
print "<body>"
print "<p>Hello World!</p>"
print "</body>"
print "</html>"
這是另一個啟動服務器的腳本:
import BaseHTTPServer
import CGIHTTPServer
import cgitb; cgitb.enable() ## This line enables CGI error reporting
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8000)
handler.cgi_directories = [""]
httpd = server(server_address, handler)
httpd.serve_forever()
啟動服務器后,當我導航到http://localhost:8000/test_cgi.py
而不是顯示HTML內容的瀏覽器時,它會顯示腳本的整個內容。 有任何想法嗎?
弄清楚了 - 我在CGI腳本的開頭缺少了解釋器行。
您需要更改handler.cgi_directories
以指向包含cgi腳本的目錄。
假設您已將文件存儲在名為cgi-bin
的文件夾中,以下操作應該可以解決問題:
handler.cgi_directories = ['/cgi']
您已使用當前工作目錄<some-path>\\cgi-bin
啟動服務器。 這對CGIHTTPServer無效。 相反,從較高的一級開始(即<some-path>
)。
也:
http://localhost:8000/cgi-bin/test_cgi.py
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.