MozPython embeds the Python interpreter in Mozilla. With MozPython you can execute Python scripts in Mozilla. Output sent to sys.stdout by a script is displayed in Mozilla's browser window, where it is interpreted as HTML.
If you have written a Web application with Python CGI scripts you can now have your application be executed offline running the same scripts in Mozilla.
In this respect MozPython is similar to MozPHP. But with MozPython you can do more. If you have PyXPCOM installed on your system you can access Mozilla internals from your Python scripts.
Copyright (c) 2004 Dr. Thomas Schilz
MozPython is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
MozPython is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
MozPython has been developed and tested with Mozilla 1.5 under Linux and Windows. It won't work with versions of Mozilla older than 1.3 and it has not been tested with other operating systems.
The Mozilla 1.5 binaries should work with Mozilla Firebird 0.7 too.
Of course you also need to have Python installed on your system.
Download the MozPython-x_y_z_win32.zip suitable for your Mozilla and Python version and extract it in your Mozilla installation directory. You should now find the following files in the components directory:
Make sure that Python is installed on your system and that the Python DLL is in your PATH. (For example, the DLL for Python 2.3 is named python23.dll.)
Download MozPython-x_y_z_linux.zip suitable for your Mozilla and Python version and extract it in your Mozilla installation directory. You should now find the following files in the components directory:
Make sure that Python is installed on your system and that the Python shared library (eg. libpython2.3.so.1.0) is in your library search path.
Close any open Mozilla windows. Backup and then delete compreg.dat and xpti.dat in the components directory.
Restart Mozilla. As a simple test if MozPython has been installed correctly download test.zip and extract it in some directory DIR on your local hard disk. Then open cfh://DIR/test.py. As a result you should see something like this:
Now click at the GET Test
link, and the result should look as follows:
Finally enter some text, eg. foo
into the input field and click at
the POST Test
button. Now you should get this:
Open your Python script files using the cfh protocol as shown above. They must have the extension .py.
Output of a script to sys.stdout is collected and upon the script's completion, is sent to the browser window. Output to sys.stdout is expected to be HTML. Output to sys.stderr is sent to the JavaScript console.
If the script terminates with a non zero exit status an error message is
displayed in the JavaScript console.
Environment variables
MozPython sets some environment variables that are normally set by a
Web server when executing a CGI script. One of them is
SERVER_SOFTWARE which is set to MozPython
.
The mozpython built-in module
MozPython adds a built-in module mozpython to Python. So
besides the SERVER_SOFTWARE variable mentioned above,
another way to
determine if a script is running in Mozilla is to try importing this module.
It has an attribute version providing access to the MozPython version.
PyXPCOM support
If you have PyXPCOM installed on your system, then MozPython will add an attribute docShell to the mozpython module. It provides access to the nsISupports interface of the browser window's nsDocShell instance. docShell can be queried for other interfaces to get access for example to the Mozilla GUI.
test2.py in test.zip is a very simple sample script. It prints the URI of the script and the navigator XUL document and resizes the document window inside the browser window. Load cfh://DIR/test2.py, and the result should look similar to this:
PyXPCOM is part of the Mozilla sources but it is not compiled into the standard Mozilla distribution. For your convenience I've put binaries for Linux and Windows into the downloads directory. The naming scheme is similar to that of the MozPython binaries. Extract the appropriate Zip file for your system to your Mozilla installation directory.
For more information about PyXPCOM see ActiveState's PyXPCOM page.
If you have a problem using or compiling MozPython, please read the FAQ. If you can't find a solution for your problem there, try to find out what is going wrong by reading the sources. If you find a solution please send the modifications you made to mozpython@web.de. If you really can't fix the problem yourself, send an email to mozpython@web.de with a detailed description of the problem and of the computing environment you are working with.
Your help is appreciated in the further development of MozPython.