A Python htdigest Generator

I've recently been testing CherryPy 3.2.0 and looking into their new tool-based support for Digest Authentication. But I didn't have a simple way to generate the correctly formatted lines for CherryPy's built-in get_ha1_file_htdigest function that expects an Apache-styled htdigest-like credentials store file.

Until now.

# file: htdigest.py
import sys
from hashlib import md5

class Options(object):

def main(opts):
    ht5 = lambda x: md5(':'.join(x)).hexdigest()
    htpwd = ':'.join((opts.user, opts.realm,
                     ht5((opts.user, opts.realm, opts.pwd))))
    print htpwd

if __name__ == '__main__':

    usage = '''
usage: python htdigest.py --user USER --pwd PASSWORD [--realm REALM]

    opts = Options()
    for o in ['--user', '--pwd', '--realm']:
            setattr(opts, o.replace('--',''), sys.argv[sys.argv.index(o)+1])
        except ValueError:
        except IndexError:
    opts.realm = getattr(opts, 'realm', 'default_realm')

    if not hasattr(opts, 'user') or not hasattr(opts, 'pwd'):
        print("[ERROR] must supply both a username and password")


which generates results like:

C:\tools>python htdigest.py --user remi --pwd lerante


While the helper script simply outputs to stdout, you can use your shell redirectors > and >> to create a new, or append to an existing, htdigest-like credentials store file.

Building Python Exts On Windows

I mainly use distribute or pip to manage Python packages on my Windows machines. And I'm pretty happy eventhough they don't provide many of the features of other package managers.

Nice, but what does that have to do with building Python extensions?

Two very nice Python-based templating libraries Jinja2 and Mako can take advantage of the native code module in the MarkupSafe for increased performance. But you need a way to build MarkupSafe on your Windows system.

Enter the DevKit from the RubyInstaller project. While the DevKit's primary goal is to enable Ruby-on-Window's users to easily build native Ruby extensions, it's also handy as a general purpose Windows-based C/C++ toolchain.

But you also need to find a way to tell pip to use the DevKit toolchain. Running pip install --help shows the --install-option and --global-option options, but there appears to be no way to tell pip which compiler toolchain to use.

Or is there?

First, create the following file in your %USERPROFILE% directory:

# file: %USERPROFILE%/pydistutils.cfg
compiler = mingw32

...and then execute the following in your shell. Pip uses the DevKit to build and install MarkupSafe and it's native library.

Adding the DevKit to PATH...

C:\Users\Jon>pip install markupsafe
Downloading/unpacking markupsafe
  Downloading MarkupSafe-0.11.tar.gz
  Running setup.py egg_info for package markupsafe

Installing collected packages: markupsafe
  Running setup.py install for markupsafe

    building 'markupsafe._speedups' extension
    C:\DevKit\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python27\include -IC:\Python27\PC -c markupsafe/_speedups.c -o build\temp.win32-2.7\Release\markupsafe\_speedups.o
    C:\DevKit\mingw\bin\gcc.exe -mno-cygwin -shared -s build\temp.win32-2.7\Release\markupsafe\_speedups.o build\temp.win32-2.7\Release\markupsafe\_speedups.def -LC:\Python27\libs -LC:\Python27\PCbuild -lpython27 -lmsvcr90 -o build\lib.win32-2.7\markupsafe\_speedups.pyd
Successfully installed markupsafe
Cleaning up...


UPDATE: This is a known issue with pip on Windows so add your support to the fix request.

Lua (1)
Python (2)
Go (1)
Ruby (7)