urllib redirect

Georg Mischler schorsch at schorsch.com
Wed Apr 19 12:34:36 CEST 2000

Anthony J Wilkinson wrote:
> On Tue, 18 Apr 2000, Thomas wrote:
> > I want one of my functions to redirect the URL.
> > (ie I call the script and off I go to www.spam.com)
> >
> > So, here's what I get, and have not overcome yet:
> >
> > import urllib
> > def simpleredirect() :
> >     ############
> >      print "Content-type: text/html"
> >      print urllib.urlopen('http://www.spam.com').read()
> >
> > simpleredirect()
> You really aren't redirecting but attempting to get the data yourself.
> If you just print out the following you can use the browser's own
> redirection facility.
> Content-type: text/html
> <html>
> ...
> </html>

This can be done in a much simpler way, whitout sending any
acutal page data to the client:

  your_new_path = 'http://www.spam.com/'

  print 'Status: 302 Redirected'
  print 'Location: %s' your_new_path

As soon as the browser (works with *every* browser!) sees the
"Location:" header, it will stop and try fetching the new page.

Unless the original poster didn't really want redirection
as claimed in the text, but rather a proxy service as hinted
at in the code. I think the latter is only recommendable in
very special circumstances.

Have fun!


Georg Mischler  --  simulations developer  --  schorsch at schorsch.com
+schorsch.com+  --  lighting design tools  --  http://www.schorsch.com/

Sent via Deja.com http://www.deja.com/
Before you buy.

More information about the Python-list mailing list