Redirects
Sometimes servers move their content around. They also make some content obsolete and put up new stuff in a different location. Sometimes they'd like us to use the more secure HTTPS protocol instead of HTTP. In all these cases, they may get traffic that asks for the old URLs, and in all these cases they'd probably prefer to be able to automatically send visitors to the new ones.
The 300 range of HTTP status codes is designed for this purpose. These codes indicate to the client that further action is required on their part to complete the request. The most commonly encountered action is to retry the request at a different URL. This is called a redirect.
We'll learn how this works when using urllib
. Let's make a request:
>>> req = Request('http://www.gmail.com') >>> response = urlopen(req)
Simple enough, but now, look at the URL of the response:
>>> response.url 'https://accounts.google.com/ServiceLogin?service=mail&passive=true&r m=false...'
This...