Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http Redirection code 3XX in python requests

I am trying to capture http status code 3XX/302 for a redirection url. But I cannot get it because it gives 200 status code.

Here is the code:

import requests r = requests.get('http://goo.gl/NZek5') print r.status_code 

I suppose this should issue either 301 or 302 because it redirects to another page. I had tried few redirecting urls (for e.g. http://fb.com ) but again it is issuing the 200. What should be done to capture the redirection code properly?

like image 629
Bishwash Avatar asked Mar 03 '14 14:03

Bishwash


People also ask

How do you get a redirected URL request in Python?

Use Python urllib Library To Get Redirection URL.request module. Define a web page URL, suppose this URL will be redirected when you send a request to it. Get the response object. Get the webserver returned response status code, if the code is 301 then it means the URL has been redirected permanently.

What is redirect HTTP code?

The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header.

What does a 30x redirect response from the server do?

A 301 redirect is a server-side redirect which redirects users from URL A to URL B, while signaling to search engines that URL A's content has been permanently moved to URL B.

How many types of redirection are there?

There are five types of redirects: 301, 302, 303, 307, and 308.


1 Answers

requests handles redirects for you, see redirection and history.

Set allow_redirects=False if you don't want requests to handle redirections, or you can inspect the redirection responses contained in the r.history list.

Demo:

>>> import requests >>> url = 'https://httpbin.org/redirect-to' >>> params = {"status_code": 301, "url": "https://stackoverflow.com/q/22150023"} >>> r = requests.get(url, params=params) >>> r.history [<Response [301]>, <Response [302]>] >>> r.history[0].status_code 301 >>> r.history[0].headers['Location'] 'https://stackoverflow.com/q/22150023' >>> r.url 'https://stackoverflow.com/questions/22150023/http-redirection-code-3xx-in-python-requests' >>> r = requests.get(url, params=params, allow_redirects=False) >>> r.status_code 301 >>> r.url 'https://httpbin.org/redirect-to?status_code=301&url=https%3A%2F%2Fstackoverflow.com%2Fq%2F22150023' 

So if allow_redirects is True, the redirects have been followed and the final response returned is the final page after following redirects. If allow_redirects is False, the first response is returned, even if it is a redirect.

like image 181
Martijn Pieters Avatar answered Oct 20 '22 05:10

Martijn Pieters