HTTP redirect via Python?

Erno Kuusela erno-news at
Tue Mar 27 10:24:03 CEST 2001

In article <SNVv6.128157$tP3.2040832 at>, "John
E. Barham" <bezilla at> writes:

| Apologies if this is too basic a question, but I'm wondering how I can send
| a redirect/URL rewrite command to a browser via Python.

| The context:  we're developing a fairly substantial Web-based application
| and using Python for the CGI coding (which is working beautifully, BTW).  In
| order to partially protect ourselves (and the users ;) against
| back/forward/refresh with forms, we'd like to send them to a "safe" URL
| after a form has been submitted.  How can we do this?  There doesn't appear
| to be anything to do this in module cgi...

| If it's a server dependent thing, we're using Apache.

def print_redirect(url):
        print 'Status: 302 Moved Temporarily'
        print 'Location:', url
        print 'Pragma: no-cache'
        print 'Content-Type: text/html'
        print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML//EN">'
        print '<title>Redirect (302)</title>'
        print '<h1>302 Moved Temporarily</h1>'
        print '<p>The answer to your request is located at'
        hurl = cgi.escape(url, 1)
        print '<a href="%s">%s</a>.' % (hurl, hurl)

note that the url must be absolute.

  -- erno

More information about the Python-list mailing list