urllib2

Clarence Gardner clarence at netlojix.net
Thu Aug 7 19:00:18 EDT 2003


I must not be getting the point of this from the documentation.
I want to connect to a URL and see what it redirects to. I built
a redirect handler that does nothing much (for testing) and installed
it thusly:
  import urllib2

  class R(urllib2.HTTPRedirectHandler):
      def redirect_request(req,fp,code,msg,hdrs):
              print "%s, %s, %s, %s, %s" % (req,fp,code,msg,hdrs)
              raise urllib2.HTTPError

  o = urllib2.build_opener(R())
  urllib2.install_opener(o)
  f = urllib2.urlopen('http://sony.com/walkmanreg')
  print f.read()[:200]

but instead of printing the message from my redirect handler, it prints
out the text of the redirected-to URL.

Any help appreciated.
clarence at silcom.com





More information about the Python-list mailing list