[FRPythoneers] FastCGI + Byte compilation

Clementson, Bill Bill_Clementson at jdedwards.com
Tue May 22 08:48:22 MDT 2001

Sean suggested one solution - here is another option that might work for you
(it's not my idea & I don't know if it will work in your environment, so
caveat emptor ;-)):


Development Tricks (from http://www.lyra.org/greg/python/)
Executable .pyc files 
Ever wanted to drop a .pyc file right into your web server's cgi-bin
directory? Frustrated because the OS doesn't know what to do with a .pyc?
(missing the #! line) 

Look no further! :-) Below is a tiny Bash script to do this. "cat" your .pyc
onto the end of this and drop it wherever you need direct execution of that

exec - 3< $0 ; exec python -c 'import os,marshal ; f = os.fdopen(3, "rb") ;
f.readline() ; f.readline() ; f.seek(8, 1) ; _c = marshal.load(f) ; del os,
marshal, f ; exec _c' $@

Make sure that the script is just two lines -- that second line is a long

Theory of operation: 

The first exec opens the script on file descriptor 3, within the context of
the Bash script. 
The second exec starts up Python, passing it a small script specified on the
The command-line script first wraps a Python file object around file
descriptor 3 (which is the Bash script). 
The script then skips past the Bash script source and the first 8 bytes of
the appended .pyc file (the eight bytes are a header). 
The script then loads the code object from the appended .pyc file and
executes it. 
The execution of the code object is what runs your script. 
Note that stdin, stdout, and stderr are all retained according to what the
caller of the Bash script set up. Also note that sys.argv has the correct
set of arguments. 

Differences from normal execution of the .pyc file: 

sys.argv[0] contains '-c' rather than the script name. 
_c has been injected into the script's namespace. 

-----Original Message-----
Date: Mon, 21 May 2001 10:31:58 -0600
From: Ken Kinder <kkinder at tridog.com>
Organization: Tridog Interactive, Inc.
To: pythoneers <frpythoneers at lists.community.tummy.com>
Subject: [FRPythoneers] FastCGI + Byte compilation
Reply-To: frpythoneers at lists.community.tummy.com

We have these source code hiding managers who want to hide our programs
source code using byte-compilation (yes, we are aware of the decompilers
and whatnot). I've developed the program fine in source code fine, using
fastcgi. But with byte-compilation, I'm not sure how to make the Apache
webserver use a .pyc file and know what to do with it?

Does anyone have any suggestions on making the webserver process pyc
files and does it work with mod_fastcgi?


Ken Kinder
Tridog Interactive, Inc.

More information about the FRPythoneers mailing list