1

我有一个依赖于 CGI 脚本的 Python 程序,当我使用 CGIHTTPServer 启动 Python BaseHTTPServer 时该程序可以工作。但我希望在不安装 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.py根目录和 cgi 脚本中应该看起来像 wwwroot\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 回答