Peuchele bio photo

Peuchele

Pedí pollo que yo pago la diferencia!

Email
Python
Photo Credit: Wikimedia Commons

Photo Credit: Wikimedia Commons

If you have a Mac OS X Leopard on an Intel platform, and you are interested in installing mod_python using your default apache setup and the python that comes with the Framework, even more if you want to give PyCAPTCHA a try together with mod_python, then check out this post! I’m covering also the installation of required libraries for PyCAPTCHA under Leopard (FreeType2, libjpeg, PIL)

mod_python

Compile mod_python

If you have Apple’s Xcode installed, compiling mod_python for Leopard to use with the system provided versions of httpd, python, etc. should be as simple as running these commands:

mkdir -p ~/mod_python
cd ~/mod_python
svn co http://svn.apache.org/repos/asf/quetzalcoatl/mod_python/trunk mod_python-trunk
cd mod_python-trunk
./configure --with-apxs=/usr/sbin/apxs
make
sudo make install
cd test/
python test.py

Original source

Configure Apache to load mod_python

Open /private/etc/apache2/httpd.conf with your favorite editor and add the following line:

LoadModule python_module libexec/apache2/mod_python.so
Example of how to configure Apache to load a mod_python application

This is just an example, I recommend reading the mod_python manual for more information.

Open /private/etc/apache2/users/username.conf with your favorite editor and add:

Alias / "/Users/username/htdocs/"
<Directory "/Users/username/htdocs/">
	SetHandler python-program
	PythonHandler mod_python.publisher
	# Disable following line when in production
	PythonDebug On
	<Files ~ "\.(gif|jpg|png|ico|js|css)$">
		SetHandler default-handler
	</Files>
	Order allow,deny
	Allow from all
</Directory>

Restart Apache with the command below:

/usr/sbin/apachectl restart

PyCAPTCHA

In order to build and install PyCAPTCHA you’ll need to compile, or make sure that you have these required libraries:

  • FreeType2
  • libjpeg
  • PIL (Python Imaging Library)

Let’s see how to set up these libraries under a Leopard / Intel environment.

FreeType2

This library is required for PyCAPTCHA, as far as I know it is already installed with the SDK, to check the directory where the library is installed use the command:

locate libfreetype.dylib

A version of this library should appear under /Developer/SDKs/MacOSX10.5.sdk/usr/X11/

libjpeg

Download jpeg source and uncompress it in any directory, e.g. ~/jpeg-6b then:

cd ~/jpeg-6b
chmod -R 777 *
./configure

Edit Makefile and change the following line from:

CFLAGS= -O2 -I$(srcdir)

to:

CFLAGS= -arch ppc -arch i386 -arch ppc64 -arch x86_64 -O2 -I$(srcdir)

Finally, build and install:

make
sudo make install-lib

PIL

Download PIL source and uncompress it in any directory, e.g. ~/imaging-1.1.6 then:

cd ~/imaging-1.1.6

Edit setup.py and change the following lines from:

    FREETYPE_ROOT = None

to (note that I’m using the FreeType2 directory discovered previously with the locate command):

    FREETYPE_ROOT = libinclude("/Developer/SDKs/MacOSX10.5.sdk/usr/X11/")

and then following line from:

    JPEG_ROOT = None

to:

    JPEG_ROOT = libinclude("/usr/local")

Locate these lines:

from distutils import sysconfig
from distutils.core import Extension, setup
from distutils.command.build_ext import build_ext

And add the following lines (correctly indented from column 0):

OrigExtension = Extension

def Extension(*args, **kwargs):
    extra_args = ['-arch', 'ppc', '-arch', 'ppc64', '-arch', 'i386', '-arch', 'x86_64']
    kwargs['extra_compile_args'] = extra_args + kwargs.get('extra_compile_args', [])
    kwargs['extra_link_args'] = extra_args + kwargs.get('extra_link_args', [])
    return OrigExtension(*args, **kwargs)

Now proceed to compile and install the library:

sudo python setup.py install

Install PyCAPTCHA

Now you are ready to build and install PyCAPTCHA. Checkout the source code from this link and run the following command:

sudo python setup.py install

That’s it!