Thursday, November 02, 2006

Server Side 301, 302 HTTP Response Header Redirect in python

Today while working on my FuckProxy project, I got totally frustrated while writing the code for 301 redirect http response header in python.
PHP has this small and easy to use code for it:-

header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$location);
But for python no such function exists. Moreover I spent 2-3 hrs on net searching for it. But there was no exact answer. Finally by several hit-and-trials and using the information collected from net, I could do it:-
print "Status: 301"
print "Location: " + location
print
sys.exit(1)
Of course, you must include sys using "import sys" statement.
For 302 (temporary redirect) just replace 301 to 302 in above code.
Thats it.
Done!!

TechnoratiTechnorati: , ,

2 comments:

Unknown said...

Thanks, I was looking for a way to redirect my appspot site to my main website with python. This helped a lot!

Cheers,

David Webb

Anonymous said...

Many thanks for publishing this. That "Status" trick is hard to find among all the web pages on how to do this in PHP, Perl, Cold Fusion, blah blah blah.