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');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:-
header('Location: '.$location);
print "Status: 301"Of course, you must include sys using "import sys" statement.
print "Location: " + location
sys.exit(1)
For 302 (temporary redirect) just replace 301 to 302 in above code.
Thats it.
Done!!
2 comments:
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
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.
Post a Comment