Python HTTP Client Request

Python HTTP module defines the classes which provide the client-side of the HTTP and HTTPS protocols. In most of the programs, the HTTP module is not directly used and is clubbed with the urllib module to handle URL connections and interaction with HTTP requests. Today we will learn how to use a Python HTTP client to fire HTTP request and then parse response status and get response body data.

Python HTTP Client

In this post on python HTTP module, we will try attempting making connections and making HTTP requests like GET, POST and PUT. Let’s get started.

Making HTTP Connections

We will start with the simplest thing HTTP module can do. We can easily make HTTP connections using this module. Here is a sample program:

Let’s see the output for this program:
python-create-connection
In this script, we connected to the URL on Port 80 with a specific timeout.

Python HTTP GET

Now, we will use HTTP client to get a response and a status from a URL. Let’s look at a code snippet:

In above script, we used a URL and checked the status with the connection object. Let’s see the output for this program:
python-create-connection
Remember to close a connection once you’re done with the connection object. Also, notice that we used a HTTPSConnection to establish the connection as the website is served over HTTPS protocol.

Getting SSL: CERTIFICATE_VERIFY_FAILED Error?

When I first executed above program, I got following error related to SSL certificates.

From the output, it was clear that it has to do something with the SSL certificates. But website certificate is fine, so it has to be something with my setup. After some googling, I found that on MacOS, we need to run Install Certificates.command file present in the Python installation directory to fix this issue. Below image shows the output produced by this command execution, it looks like it’s installing latest certificates to be used when making SSL connections.

Python HTTP GET

Note that I got this error on Mac OS. However, on my Ubuntu system, it worked perfectly fine.

python-http-client-ubuntu

 

Python HTTP Client Ubuntu

From the response we receive, the headers usually also contain important information about the type of data sent back from the server and the response status as well. We can get a list of headers from the response object itself. Let’s look at a code snippet which is a little-modified version of the last program:

Let’s see the output for this program:

python-http-client-ubuntu

Python HTTP POST

We can POST data to a URL as well with the HTTP module and get a response back. Here is a sample program:

Let’s see the output for this program:
python-http-client-ubuntu
Feel free to use the HTTP Bin library to try more requests.

Python HTTP PUT Request

Of course, we can also perform a PUT request using the HTTP module itself. We will use the last program itself. Let’s look at a code snippet:

Let’s see the output for this program:

python-http-client-ubuntu

Conclusion

In this lesson, we studied simple HTTP operations which can be done using http.client. We can also create python http server using SimpleHTTPServer module.

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: