473,240 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

Problem with py2exe-frozen CGIHttpServer-based script

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()


Jul 18 '05 #1
3 2392
"vincent wehren" <vi*****@visualtrans.de> 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
Jul 18 '05 #2
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
Jul 18 '05 #3
"Thomas Heller" <th*****@python.net> schrieb im Newsbeitrag
news:is**********@python.net...
| "vincent wehren" <vi*****@visualtrans.de> 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

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hellprout | last post by:
hi , i want to use py2exe or installer5b5_5 with my python source so no problem when i execute the source , but when i use installer or py2exe to create an exe file on windows , i have some...
4
by: bwaha | last post by:
First time trying to create an executable with py2exe. I have a small program which makes use of python23 (2.3.5?), wxpython ('2.6.2.1'), matplotlib ('0.83.2'), win32com (latest?), Numeric...
6
by: Michele Petrazzo | last post by:
Hi list, just found in this moment that my applications stop to work with win xp and receive this error: """ This application has requested the Runtime to terminate it in an unusual way....
1
by: Dave Lim | last post by:
>On May 3, 1:29 pm, Dave Lim <diband... at yahoo.com> wrote: site:http://surguy.net/articles/speechrecognition.xml used out tried ? to protection aroundhttp://mail.yahoo.com I went and
5
by: msunderwd | last post by:
Having a problem with "compiling" a Tkinter/python program using py2exe (and pyinstaller, for that matter)... I have several dialogs that are derived from the tkSimpleDialog.Dialog class. These...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.