Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Py2Exe本地服务器不执行CGI_Python_Cgi_Py2exe - Fatal编程技术网

Python Py2Exe本地服务器不执行CGI

Python Py2Exe本地服务器不执行CGI,python,cgi,py2exe,Python,Cgi,Py2exe,我有一个特定的Python程序,它依赖于CGI脚本,当我用CGIHTTPServer启动一个基于Python的HttpServer时,它就可以工作了。但我希望所有这些都在不安装Python的情况下运行,所以我使用了Py2Exe 我设法从我的脚本中创建了一个.exe,它在执行时确实创建了一个工作的本地web服务器。然而,CGI脚本只显示为代码,不执行 以下是整个服务器脚本,它还启动默认浏览器: #!/usr/bin/env python import webbrowser import Base

我有一个特定的Python程序,它依赖于CGI脚本,当我用CGIHTTPServer启动一个基于Python的HttpServer时,它就可以工作了。但我希望所有这些都在不安装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_目录中尝试了几个不同的版本,以防万一…

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

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

在启动服务器之前先启动浏览器Hehe,不,这不是答案,但谢谢。当Python调用该脚本时,该脚本可以工作——浏览器需要一段时间来初始化,因此httpd在那时已经开始运行了。但是,如果我在“永远服务”之后启动浏览器,则永远不会调用该行。无论如何,一旦服务器启动并运行,我可能会手动打开一个浏览器窗口并浏览到该地址,结果是相同的:我看到CGI代码,它没有被执行。在lo inerface上运行tcpdump并查看请求和响应。我想我也可以用Firebug做到这一点,对吗?但我不知道你的意思是什么。我在找什么?CGI脚本没有执行-这是服务器问题,对吗?与Apache中未正确配置.htaccess类似。然而,我怀疑这与Python如何解释CGI有关。也许它会调用自身(Python),这在.exe中不起作用,因为。。。嗯,没有找到Python。我认为更可能是webroot路径问题。如果是python,你会得到一个错误,而不是文件。你在启动服务器之前启动浏览器Hehe,不,这不是答案,但谢谢。当Python调用该脚本时,该脚本可以工作——浏览器需要一段时间来初始化,因此httpd在那时已经开始运行了。但是,如果我在“永远服务”之后启动浏览器,则永远不会调用该行。无论如何,一旦服务器启动并运行,我可能会手动打开一个浏览器窗口并浏览到该地址,结果是相同的:我看到CGI代码,它没有被执行。在lo inerface上运行tcpdump并查看请求和响应。我想我也可以用Firebug做到这一点,对吗?但我不知道你的意思是什么。我在找什么?CGI脚本没有执行-这是服务器问题,对吗?与Apache中未正确配置.htaccess类似。然而,我怀疑这与Python如何解释CGI有关。也许它会调用自身(Python),这在.exe中不起作用,因为。。。嗯,没有找到Python。我认为更可能是webroot路径问题。如果是python,则会出现错误,而不是文件。