[Twisted-Python] How to do basic authentication on twisted web

Reza Lotun rlotun at gmail.com
Mon Aug 17 08:24:39 MDT 2009


Hi Chris,

> Automatic daemonization and monitoring? This sounds like exactly what I'm after.
>
> I've been calling twisted like so for a project: explicitly setting
> the process id, logger and source file here. And relying on a separate
> cron script to check if all is well:
>
> twistd --pidfile=$PIDFILE --syslog --prefix=program_name --python
> program_name.py
>
> Would using a .tac file make some of these flags redundant?
>
> All I could found about .tac files is here -
> http://twistedmatrix.com/projects/core/documentation/howto/application.html
>
> Are there any other resources where I can found out about the
> advantages of using .tac files instead the way I've bee doing stuff?

Well, it won't make all the flags redundant - the default .pid file is
still twistd.pid, and you'd have to set your --prefix, etc. However,
you definitely should be using .tac files since you can handle
daemonization through twistd by using the application framework (you
can select a reactor from twistd by using the -r flag - for example
`twistd -r epoll -y myapp.tac --pidfile=...`).

The link you've posted is the de-facto source for .tac file
documentation. I can post a simple .tac file used for an internal web
tool:

---
from twisted.application import service, internet
from twisted.internet import defer
from twisted.python.log import ILogObserver, FileLogObserver
from twisted.python.logfile import DailyLogFile
from twisted.web import guard, server
from twisted.cred.portal import Portal
from twisted.cred.checkers import FilePasswordDB

from tdusers.lookup import SimpleRealm

import crypt

def cmp_pass(uname, password, storedpass):
    return crypt.crypt(password, storedpass[:2])

def get_api_service():
    """ Return a service suitable for creating an application object. """
    checkers = [FilePasswordDB('/home/repo/.htpasswd',
                hash=cmp_pass)]
    wrapper = guard.HTTPAuthSessionWrapper(
                    Portal(SimpleRealm(), checkers),
                    [guard.BasicCredentialFactory('auth')])
    return internet.TCPServer(8080, server.Site(resource=wrapper))

application = service.Application('Lookup')
logfile = DailyLogFile('lookup.log', '.')
application.setComponent(ILogObserver, FileLogObserver(logfile).emit)

# attach the service to its parent application
service = get_api_service()
service.setServiceParent(application)
----

I then start my tool using twistd -r epoll -y mytacfile.tac. The good
thing about this approach is that your code can live in a seperate
file and no mention of the *running* of it has to be made in that file
- that is, you shouldn't have to explicity choose what reactor you're
using since that's more of a deployment question.

I don't really know of a more comprehensive source for twistd and .tac
files - I suppose if you fancy dong a writeup that'd be appreciated by
the rest of us ;-)

Cheers,
Reza

-- 
Reza Lotun
mobile: +44 (0)7521 310 763
email:  rlotun at gmail.com
work:   reza at tweetdeck.com
twitter: @rlotun




More information about the Twisted-Python mailing list