python - Py2Exe本地服务器不执行CGI

标签 python cgi py2exe

我有一个依赖于 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 中尝试了几个不同的版本,以防万一......

最佳答案

问题是 py2exe 只将您的服务器脚本转换为 exe,所有 cgi 脚本仍然是 .py 并且它们需要安装 python 才能运行。尝试转换 'cgi' 目录中的每个脚本。
假设您的根目录中有server.py,cgi脚本位于wwwroot\cgi-bin,您的setup.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

关于python - Py2Exe本地服务器不执行CGI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12154819/

相关文章:

python - 使用 pip install --editable 更改 .egg-info 目录

python - sklearn.impute.IterativeImputer 的实现

python - 重要的包和模块与 py2exe 不兼容?

python - pymssql 抛出 ImportError : No module named _mssql when build app with py2exe

python:提供的参数太多

python - 在 Python 的 Numpy 中,点积不等同于 einsum,我不确定为什么不

javascript - 多页复选框处理

python - 如何下载位于 Web 服务器 CGI-bin 文件夹中的文件?

html - 将当前用户与登录计算机的员工列表进行比较 Perl、CGI

python - 如何将pyw文件转换成exe文件?