Problem with py2exe-frozen CGIHttpServer-based script

Discussion in 'Python' started by vincent wehren, Nov 27, 2003.

  1. Hi,

    as a small capabilities demo I coded the piece below to show how to use
    Python for cgi'ing on localhost and it more or less does the trick :).
    However, I when I freeze it with py2exe, starting the resulting exe fires up
    the server allright,
    but fails execute cgi commands correctly (i.e. the expected output - let's
    say from cgi.test()) - is no longer emitted to the browser...).

    Is there some py2exe-magic I need to do that I don't know of? Something in
    the code that prevents the frozen version to work?


    Any pointers would be much appreciated...

    Python 2.3.2 , Py2exe 0.4.2, Win XP

    Here's the code...:


    import CGIHTTPServer, BaseHTTPServer, SimpleHTTPServer
    import threading
    import sys

    port=8000

    # nicked from the SimpleHTTPServer test rig
    def simple( HandlerClass = SimpleHTTPServer.SimpleHTTPRequestHandler
    ,ServerClass = BaseHTTPServer.HTTPServer):
    """
    """

    base(HandlerClass, ServerClass)


    # nicked from the BaseHTTPServer test rig
    def base(HandlerClass = BaseHTTPServer.BaseHTTPRequestHandler,
    ServerClass = BaseHTTPServer.HTTPServer, protocol="HTTP/1.0"):
    """
    """

    server_address = ('localhost', port)
    HandlerClass.protocol_version = protocol
    try:
    httpd = ServerClass(server_address, HandlerClass)
    sa = httpd.socket.getsockname()
    print "Serving HTTP on", sa[0], "port", sa[1], "..."
    httpd.serve_forever()
    except KeyboardInterrupt:
    http.socket.close()



    def RunServer(readyEvent=None
    ,HandlerClass = CGIHTTPServer.CGIHTTPRequestHandler
    ,ServerClass = BaseHTTPServer.HTTPServer):

    simple(HandlerClass, ServerClass)
    if readyEvent:
    readyEvent.set()

    def main():

    testServerReady = threading.Event()
    threading.Thread(target=RunServer, args=(testServerReady,)).start()
    testServerReady.wait()


    if __name__ == '__main__':
    main()
    vincent wehren, Nov 27, 2003
    #1
    1. Advertising

  2. "vincent wehren" <> writes:

    > Hi,
    >
    > as a small capabilities demo I coded the piece below to show how to use
    > Python for cgi'ing on localhost and it more or less does the trick :).
    > However, I when I freeze it with py2exe, starting the resulting exe fires up
    > the server allright,
    > but fails execute cgi commands correctly (i.e. the expected output - let's
    > say from cgi.test()) - is no longer emitted to the browser...).
    >
    > Is there some py2exe-magic I need to do that I don't know of? Something in
    > the code that prevents the frozen version to work?


    That's an easy one!
    Look into CGIHTTPServer.py, near line 232:
    if self.is_python(scriptfile):
    => interp = sys.executable
    if interp.lower().endswith("w.exe"):
    # On Windows, use python.exe, not pythonw.exe
    interp = interp[:-5] + interp[-4:]
    cmdline = "%s -u %s" % (interp, cmdline)

    It tries to start 'sys.executable' with the python cgi script. Normally
    sys.executable is the Python interpreter, but for a py2exe'd script this
    is the running executable (which is no longer a usual Python
    interpreter).

    Changing this line to 'interp = r"c:\python23\python.exe"' makes the
    frozen script work (although then the Python installation is required
    again).

    (A few minutes later, looking at CGIHTTPServer.py again)
    It seems you have to hack this module so that the code block starting at
    line 270 is used, which says:
    else:
    # Other O.S. -- execute script in this process
    and then it works fine.

    Thomas
    Thomas Heller, Nov 27, 2003
    #2
    1. Advertising

  3. Look at this piece of code in my *modified* CGIHTTPServer.py:

    if self.is_python(scriptfile):
    # THIS DOESN'T WORK AFTER PY2EXE!
    #interp = sys.executable
    #if interp.lower().endswith("w.exe"):
    # # On Windows, use python.exe, not pythonw.exe
    # interp = interp[:-5] + interp[-4:]
    #cmdline = "%s -u %s" % (interp, cmdline)
    cmdline = "%s -u %s" % ('distpython', cmdline)

    The comments have the "original" standard library code. It uses
    sys.executable, and that returns "yourCGIProg.exe" instead of
    "Python.exe" or something like that, as you need for running the CGI
    program.

    My *modified* version works when used by some *.exe generated by
    py2exe.

    --
    Man is the only animal that blushes -- or needs to.
    -- Mark Twain
    Will Stuyvesant, Nov 27, 2003
    #3
  4. "Thomas Heller" <> schrieb im Newsbeitrag
    news:...
    | "vincent wehren" <> writes:
    |
    | > Hi,
    | >
    | > as a small capabilities demo I coded the piece below to show how to use
    | > Python for cgi'ing on localhost and it more or less does the trick :).
    | > However, I when I freeze it with py2exe, starting the resulting exe
    fires up
    | > the server allright,
    | > but fails execute cgi commands correctly (i.e. the expected output -
    let's
    | > say from cgi.test()) - is no longer emitted to the browser...).
    | >
    | > Is there some py2exe-magic I need to do that I don't know of? Something
    in
    | > the code that prevents the frozen version to work?
    |
    | That's an easy one!
    | Look into CGIHTTPServer.py, near line 232:
    | if self.is_python(scriptfile):
    | => interp = sys.executable
    | if interp.lower().endswith("w.exe"):
    | # On Windows, use python.exe, not pythonw.exe
    | interp = interp[:-5] + interp[-4:]
    | cmdline = "%s -u %s" % (interp, cmdline)
    |
    | It tries to start 'sys.executable' with the python cgi script. Normally
    | sys.executable is the Python interpreter, but for a py2exe'd script this
    | is the running executable (which is no longer a usual Python
    | interpreter).
    |
    | Changing this line to 'interp = r"c:\python23\python.exe"' makes the
    | frozen script work (although then the Python installation is required
    | again).
    |
    | (A few minutes later, looking at CGIHTTPServer.py again)
    | It seems you have to hack this module so that the code block starting at
    | line 270 is used, which says:
    | else:
    | # Other O.S. -- execute script in this process
    | and then it works fine.
    |
    | Thomas


    Hi Thomas,

    I suspected something along those lines....

    I'll give the hack a go!

    Thanks!

    Vincent
    vincent wehren, Nov 27, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Hansen

    Re: Securing PyDoc and CGIHTTPserver

    Peter Hansen, Jul 10, 2003, in forum: Python
    Replies:
    7
    Views:
    419
    Peter Hansen
    Jul 15, 2003
  2. Bill Oldroyd
    Replies:
    1
    Views:
    385
  3. 7stud

    problem with CGIHTTPServer

    7stud, Mar 28, 2008, in forum: Python
    Replies:
    2
    Views:
    896
    7stud
    Mar 29, 2008
  4. est
    Replies:
    1
    Views:
    537
  5. LC Geldenhuys
    Replies:
    5
    Views:
    249
    Robert Klemme
    Feb 18, 2004
Loading...

Share This Page