2012-08-28 94 views
1

我有一个特定的Python程序,它依赖于一个CGI脚本,它在我用CGIHTTPServer启动一个Python BaseHTTPServer时起作用。但我希望所有这些都可以在不安装Python的情况下运行,所以我使用了Py2Exe。Py2Exe本地服务器不执行CGI

我设法从我的脚本创建一个.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中尝试了几个不同的版本,以防万一...

+0

要启动浏览器启动服务器 – stark

+0

赫赫之前,不,这不是答案,但感谢。该脚本在被Python调用时起作用 - 浏览器需要一段时间才能初始化,因此httpd已经在服务。不过,如果我在'永远投放'之后启动浏览器,那么该行从未被调用过。 无论如何,一旦服务器启动并运行,我可能会手动打开一个浏览器窗口并浏览到该地址,结果相同:我看到CGI代码,它没有被执行。 – Notnasiul

+0

在loinerface上运行tcpdump并查看请求和响应。 – stark

回答

1

问题是py2exe仅将您的服务器脚本转换为exe,所有cgi脚本仍然是.py和他们需要python安装才能运行。尝试转换'cgi'目录中的每个脚本。
Assuiming你在根目录和CGI脚本在wwwroot\cgi-binsetup.pyserver.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