2

正常に動作する CGIHTTPServer を作成しました。何をしても問題はありません。Python ページはレンダリングされず、ソース コードは常にブラウザに表示されます。

pyhttpd.py

#!/usr/bin/python
import CGIHTTPServer
import BaseHTTPServer
class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    cgi_directories = [""]
PORT = 8080
httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()

cgi-bin/hello.py

#!/usr/bin/python
print 'Content-Type: text/html'
print
print '<html>'
print '<head><title>Hello</title></head>'
print '<body>'
print '<h2>Hello World</h2>'
print '</body></html>'

http://some.ip.address:8080/cgi-bin/hello.py :

#!/usr/bin/python
print 'Content-Type: text/html'
print
print '<html>'
print '<head><title>Hello</title></head>'
print '<body>'
print '<h2>Hello World</h2>'
print '</body></html>'

すべてのファイルのアクセス許可を実行可能に設定しました。.html ファイルは正常にレンダリングされます。サーバーが実行されているルート フォルダーにファイルを戻しても違いはありません。ルートと別の通常のユーザーとして実行してみました。同じ結果。

「レンダリングされていないpythonページ」をグーグルで試してみましたが、有用なものは見つかりませんでした!

編集

また、オーバーライドなしでより単純なサーバーを実行しようとしましたが、結果は同じで、pything コードはレンダリングされません。

pyserv.py

#!/usr/bin/python
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
serve = HTTPServer(("",80),CGIHTTPRequestHandler)
serve.serve_forever()
4

1 に答える 1

0

オーバーライドしたため、この問題が発生していると思いますcgi_directories

ドキュメントの関連部分は次のとおりです。

「これはデフォルトで['/cgi-bin', '/htbin']、CGI スクリプトを含むものとして扱うディレクトリを記述します。」

スクリプトをルート ディレクトリに配置するか、オーバーライドを削除してスクリプトをディレクトリにcgi_directories配置します/cgi-bin

これは、同様の簡単なセットアップを行ごとに説明する良いリンクです: https://pointlessprogramming.wordpress.com/2011/02/13/python-cgi-tutorial-1/

アップデート:

上記ページのコメントによると、設定するとcgiディレクトリ機能が無効になるcgi_directories = [""]ようです。代わりに、setcgi_directories = ["/"]を現在のディレクトリに設定します。

于 2013-02-04T02:58:47.800 に答える