HTTP codes

The HTTP protocol defines five classes of response codes to indicate the status of a request:

  • 1XX-Informational: The 100 range codes are used for informational purposes. It is only present in HTTP/1.1.
  • 2XX-Success: The 200 range of codes are used to indicate that the action requested by the client was received, understood, accepted, and processed. The most common is 200 OK.
  • 3XX-Redirection: The 300 range indicates the client that must take additional actions to complete the request. Most of these codes are used in URL redirection. The most common of this group is the 302 Found code.
  • 4XX-Client-side error: The 400 range are used to indicate that the client has had an error. The most common is 404 Not Found.
  • 5XX-Server-side error: The range 500 is used to indicate an error on the server side. The most common is 500 Internal Server Error.
We suggest you learn the different codes in each group here:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

Let's write some code. Let's open our editor in the virtual machine and create a new file:

  1. First, we import the requests library by typing import requests.
  2. We will create a variable for our target URL. We'll use httpbin.org again and type:
url='http://httpbin.org/html'
  1. Then, we'll print the response code with req.status _code. We do this by entering the following:
req = requests.get(url) 
  1. Print the response code for the req.status_code string. This can be done as follows:
print "Response code: " + str(req.status_code) 
  1. That's it! We'll save the file in /Example/Section-2 as Video-4.py and switch to the Terminal to run the script.
  2. Use python Video-4.py.

You should see a 200 status code in the response, which means that our request was successful:

Well done, let's move on.

Let's go back to the editor:

  1. Now, let's change the target URL to something that does not exist. In order to see an error code, we'll change the URL and write fail:
import requests
url='http://httpbin.org/fail'
req = requests.get(url)
print "Response code: " + str(req.status_code)
  1. Let's save it and run this script in the Terminal again.

Now, when we run the server, it will return a 404 status code, which means that the resource was not found on the server:

So, now we know that we can ask the server for a list of directories and files and find which ones exist and which ones do not. Interesting, right?

Now, let's see how we deal with redirections. We'll use an example page that will take a parameter URL and redirect us to that defined URL:

  1. Let's go back to our script and modify it in order to get a new directory called payload, which will contain the URL where we want to be redirected to.
  2. We'll use payload='url' to be redirected to www.bing.com. We can do this as follows:
payload={'url':'http://www.bing.com'} 
  1. Now, we'll use this, the resource redirect-to and add the params parameter and set it to the payload.
  2. Finally, we'll print the content with print req.text:
import requests
url='http://httpbin.org/redirect-to'
payload = {'url':'http://www.bing.com'}
req = requests.get(url,params=payload)
print req.text
print "Response code: " + str(req.status_code)
  1. We'll save it and run it again.

What do we get now? A 200 code and the content of https://www.bing.com/:

The code should be 302, right? We need to access the history of the request to see the redirects.

  1. Let's add print r.history. The history is a list of all the responses in the redirection chain. We will print the URL and the response code for each URL with this loop to our script.
  2. For x in req.history, print this status code concatenated with the URL:
import requests
url='http://httpbin.org/redirect-to'
payload = {'url':'http://www.bing.com'}
req = requests.get(url,params=payload)
print req.text
print "Response code: " + str(req.status_code)
for x in req.history:
print str(x.status_code) + ' : ' + x.url
  1. Save it and run it:

Now, we can see that before the 200, there was a 302 redirection code sending our browser to www.bing.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.128.129