为没有python的用户创建一个python exe - 可以处理服务器CGI

问题描述 投票:1回答:1

我编写了一个包含python CGI脚本的简单python localhost服务器。 CGI基于用户搜索返回html。

我想构建一个为用户创建本地主机的编译版本,并让他们运行python CGI脚本(不安装python)。

我使用pyinstaller来创建服务器程序的exe。但是,当调用CGI时,它仍然试图运行未编译的python(用户的机器不会有)。

有关如何在不安装python的情况下为用户提供CGI的任何想法?

python localhost cgi
1个回答
0
投票

我能够通过以下步骤使这个工作:

  1. 使用flask而不是简单的HTML服务器(CGIHTTPRequestHandler)。这对于提供静态文件来说是一个巨大的过度杀伤,但它确实有效。它需要最少的重构,因为flask可以使用:request.args.get获取类似于CGI的信息
  2. 使用pyinstaller编译。这首先不起作用,但生成[您的应用程序] .spec文件。
  3. 篡改spec文件,在此页面上按说明包含其他数据:https://www.quora.com/Can-I-convert-a-Flask-application-into-an-executable-file-that-runs-on-Windows-like-an-exe-file(这里肯定是一些伏都教,但它有效!)
  4. 在a.datas调用中添加了我需要的其他目录(搜索索引)。
  5. 使用修改后的spec文件而不是.py文件重新启动pyinstaller。

在步骤3中添加的代码(这是quora链接中的图片)

         hiddenimports=['email','email.message',
            'email.mime.message', 'email.mime.image',
            'email.mime.text', 'email.mime.multipart',
                'email.mime.audio', 'email.mime.multipart',
           ],

def extra_datas(mydir):
     def rec_glob(p,files):
         import os
         import glob
         for d in glob.glob(p):
             if os.path.isfile(d):
                 files.append(d)
             rec_glob("%s/*" % d, files)
     files = []
     rec_glob("%s/*" % d, files)
     extra_datas = []
     for f in files: 
         extra_datas.append((f, f, 'DATA'))
     return extra_datas

#include our data directories
a.datas += extra_datas("static")
a.datas += extra_datas("templates")
© www.soinside.com 2019 - 2024. All rights reserved.