HTTP methods

So far, we've been using requests for asking servers to send web resources to us, but HTTP provides more actions that we can perform. The GET in our request lines is an HTTP method, and there are several methods, such as HEAD, POST, OPTION, PUT, DELETE, TRACE, CONNECT, and PATCH.

We'll be looking at several of these in some detail in the next chapter, but there are two methods, we're going to take a quick look at now.

The HEAD method

The HEAD method is the same as the GET method. The only difference is that the server will never include a body in the response, even if there is a valid resource at the requested URL. The HEAD method is used for checking if a resource exists or if it has changed. Note that some servers don't implement this method, but when they do, it can prove to be a huge bandwidth saver.

We use alternative methods with urllib by supplying the method name to a Request object when we create it:

>>> req = Request('http://www.google.com', method='HEAD')
>>> response = urlopen(req)
>>> response.status
200
>>> response.read()
b''

Here the server has returned a 200 OK response, yet the body is empty, as expected.

The POST method

The POST method is in some senses the opposite of the GET method. We use the POST method for sending data to the server. However, in return the server can still send us a full response. The POST method is used for submitting user input from HTML forms and for uploading files to a server.

When using POST, the data that we wish to send will go in the body of the request. We can put any bytes data in there and declare its type by adding a Content-Type header to our request with an appropriate MIME type.

Let's look at an example for sending some HTML form data to a server by using a POST request, just as browsers do when we submitt a form on a website. The form data always consists of key/value pairs; urllib lets us work with regular dictionaries for supplying this (we'll look at where this data comes from in the following section):

>>> data_dict = {'P': 'Python'}

When posting the HTML form data, the form values must be formatted in the same way as querystrings are formatted in a URL, and must be URL-encoded. A Content-Type header must also be set to the special MIME type of application/x-www-form-urlencoded.

Since this format is identical to querystrings, we can just use the urlencode() function on our dict for preparing the data:

>>> data = urlencode(data_dict).encode('utf-8')

Here, we also additionally encode the result to bytes, as it's to be sent as the body of the request. In this case, we use the UTF-8 character set.

Next, we will construct our request:

>>> req = Request('http://search.debian.org/cgi-bin/omega', data=data)

By adding our data as the data keyword argument, we are telling urllib that we want our data to be sent as the body of the request. This will make the request use the POST method rather than the GET method.

Next, we add the Content-Type header:

>>> req.add_header('Content-Type', 'application/x-www-form-urlencode;  charset=UTF-8')

Lastly, we submit the request:

>>> response = urlopen(req)

If we save the response data to a file and open it in a web browser, then we should see some Debian website search results related to Python.

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

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