[Tutor] Redirect from a CGI script

Python python at venix.com
Sat Jan 20 04:12:58 CET 2007


On Sat, 2007-01-20 at 02:10 +0000, Paulino wrote:
> Thank you Andre,
> 
> well it doesn't work either!

This works, 

#!/usr/bin/python
print "Location:        http://python.org/\r\n\r"

as does this

#!/usr/bin/python
print "Content-type:    text/html\r"
print "Location:        http://python.org/\r\n\r"


Tested using Apache on Linux.  A redirect should have a 3xx status.
Apache saw the location header and fixed the status to be 302.  My
browser (firefox) received the 302 status with the new location and
requested the new URL.

Each header line should be separated by \r\n.  A Python print on linux
will only output \n.  In actual practice, that appears to work OK.
either the browsers tolerate the missing \r or Apache fixes the data
stream.

Firefox plugins called tamperdata and liveheaders can be very helpful
for debugging these kinds of interactions.

Get the redirect to a real web site working.  Then fix it to redirect to
your script.  Use tamperdata to see what is going on if you have trouble
making it work.

> 
> Paulino
> > Andre Engels escreveu:
> > 2007/1/18, Paulino <paulino1_at_sapo.pt at .syntax-error>: 
> >         How can i redirect to another URL from a python CGI script.
> >         
> >         Is's suposed to be as simply as:
> >         
> >         print "Location : http://newurl "
> >         It's not working.
> >         
> >         this simple code does't work - < redir.pyw>
> >         'print "Content-Type:text/html\n\n"
> >         'print "Location : /cgi-bin/ecodiv.pyw "
> >         'print
> >         
> >         I use CGIHTTPServer, the server script is as follows:
> >         
> >         'from BaseHTTPServer import HTTPServer 
> >         'from CGIHTTPServer import CGIHTTPRequestHandler
> >         'HTTPServer(("localhost", 80),
> >         CGIHTTPRequestHandler).serve_forever()
> >         
> >         instead of redirecting, it only prints
> >         'Location : /cgi-bin/ecodiv.pyw' inthe 
> >         browser
> > 
> > 
> > I haven't tested it, but I think I had a similar error recently, and
> > that was solved by removing the \n\n at the end of the Content-Type
> > line. You could try that.
> > 
> > 
> > 
> > 
> > 
> > 
> > -- 
> > Andre Engels, andreengels at gmail.com
> > ICQ: 6260644  --  Skype: a_engels
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list