[mod_python] Redirecting transhandler example.

Sean True seant at webreply.com
Thu Aug 9 11:07:31 EST 2001


httpredirector.py is a mod_python implementation of some of the Apache URL
rewriting
module. Much more primitive, intended to be easily configurable and
modifiable,
and motivated by the AT&T/Mediaone system wide blockage of port 80 in
response to
the "code red" worm attack.

Note: to be useful in the "code red" scenario, you need to be running
mod_python
on a server that is not being blocked. Of course, the code can be used (or
changed)
to do other sorts of useful URL rewriting.

We deploy httpredirector.py on a production webserver somewhere, using
wildcard
virtualhost aliases to let the redirector get hold of a host, rewrite the
host to
one of our home machines using an unblocked port, such as 8080.

-- Sean
-------
Sean True
WebReply.Com, Inc.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Sean True.vcf
Type: text/x-vcard
Size: 309 bytes
Desc: not available
Url : http://mailman.modpython.org/pipermail/mod_python/attachments/20010809/6825c027/SeanTrue-0003.vcf
-------------- next part --------------
"""
httpredirector.py is a mod_python implementation of some of the Apache URL rewriting
module. Much more primitive, intended to be easily configurable and modifiable,
and motivated by the AT&T/Mediaone system wide blockage of port 80 in response to
the "code red" worm attack.

Note: to be useful in the "code red" scenario, you need to be running mod_python
on a server that is not being blocked. Of course, the code can be used (or changed)
to do other sorts of useful URL rewriting.

We deploy httpredirector.py on a production webserver somewhere, using wildcard
virtualhost aliases to let the redirector get hold of a host, rewrite the host to
one of our home machines using an unblocked port, such as 8080.


# --- Redirector site
<VirtualHost 10.0.0.1>
    ServerName redirector
    # Set up your DNS so that your public sites come here
    ServerAlias *.yourdomain.com
    PythonInterpreter phtml
    PythonTransHandler httpredirector
    PythonOption redirects {'yourdomain.com':'yourprivatedomain:8080'}
</VirtualHost>

Authors: Peter McA'Nulty and Sean True, WebReply.Com, Inc. Copyright (c) 2001
License: Use under the terms of the Python license: http://www.python.org/2.1.1/license.html
"""

from mod_python import apache
import sys, string

debug = 0
def transhandler(req):
    def log(s):
        sys.stderr.write("httpredirector: %s\n" % s)

    # Pick up the host field from the request
    # -- if HTTP 1.0, we're going to fail here
    try:
        host = req.headers_in['Host']
    except:
        log("didn't have 'HTTP_HOST' environment")
        return apache.DECLINED
    if debug:
        log(host)

    # Get the dictionary of redirect options from
    # httpd.conf
    opts = req.get_options()
    try:    
        redirects = opts['redirects']
    except:
        log("didn't have 'redirects' PythonOption")
        return apache.DECLINED
    try:    
        redirects = eval(redirects)
    except:
        log("'redirects' was not a valid dictionary [%s]" % `redirects`)
        return apache.DECLINED

    # This shouldn't fail. Belt, suspenders, ...
    uri = req.unparsed_uri
    if not uri:
        log("didn't have 'REQUEST_URI' environment")
        return apache.DECLINED

    # For each redirection option, search the host string for a target and
    # replace it. We stop after the first match, and redirect to the result.
    # This could be a list of regular expressions instead, but this will do for
    # now.
    keys = redirects.keys()
    for k in keys:
        if string.find(host, k) != -1:
            rehost = string.replace(host, k, redirects[k])
            req.headers_out['location'] = "http://%s%s" % (rehost, uri)
            req.status = apache.HTTP_MOVED_PERMANENTLY
            req.send_http_header()
            if debug: log("%s%s => %s%s" % (host,uri, rehost,uri))
            return apache.DONE
        
    log("didn't have matching redirect for %s" % host)
    return apache.DECLINED
    
    


More information about the Mod_python mailing list