1

CGIHTTPServer で Python BaseHTTPServer を起動すると動作する CGI スクリプトに依存する特定の Python プログラムがあります。しかし、Python をインストールせずにこれらすべてを実行したいので、Py2Exe を使用しました。

スクリプトから .exe を作成することができました。実行すると、実際に動作するローカル Web サーバーが作成されます。ただし、CGI スクリプトはコードとして表示されるだけで、実行されません。

これは、デフォルトのブラウザーも起動するサーバー スクリプト全体です。

#!/usr/bin/env python

import webbrowser
import BaseHTTPServer
import CGIHTTPServer

server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8008)
handler.cgi_directories = ["/cgi"]
httpd = server(server_address, handler)
webbrowser.open_new("http://localhost:8008/cgi/script.py");
httpd.serve_forever()

ただし、その script.py は表示されるだけで実行されません。理由がわかりません。念のため、handler.cgi_directories でいくつかの異なるバージョンを試しました...

4

1 に答える 1

1

問題は、py2exe はサーバー スクリプトを exe に変換するだけであり、すべての cgi スクリプトは依然として .py であり、実行するには Python のインストールが必要です。'cgi'ディレクトリ内のすべてのスクリプトを変換してみてください。ルートディレクトリにあると
仮定すると、次のよう になりますserver.pywwwroot\cgi-binsetup.py

#!usr/bin/env python
from distutils.core import setup
import py2exe, os

setup(name='server',
    console=['server.py'],
    options={
                "py2exe":{
                        "unbuffered": True,
                        "packages": "cgi, glob, re, json, cgitb",       # list all packages used by cgi scripts
                        "optimize": 2,
                        "bundle_files": 1
                }},
    zipfile="library.zip")
os.rename("dist\\library.zip","dist\\library.zip.bak")                  # create backup of the library file

files=[]
for f in os.listdir("wwwroot\\cgi-bin"):                                # list all cgi files with relative path name
    files.append("wwwroot\\cgi-bin\\"+f)

setup(name='cgi',
    console= files,
    options={
        "py2exe":{
                        "dist_dir": "dist\\wwwroot\\cgi-bin",
                        "excludes": "cgi, glob, re, json, cgitb",       # we are going to discard this lib, may as well reduce work
                        "bundle_files": 1
                }
            },
    zipfile="..\\..\\library.zip")                                      # make sure zipfile points to same file in both cases

os.remove("dist\\library.zip")                                          # we don't need this generated library
os.rename("dist\\library.zip.bak","dist\\library.zip")                  # we have already included everything in first pass
于 2013-05-17T03:37:49.880 に答える