Python

How to Make an HTTP Client Program in Python

The HTTP protocol sends a client request to the webserver, retrieving specific data and information if the transaction is legitimate. Using many methods provided in the python request package, you could examine the server’s answer. Therefore, within this guide today, we will discuss some HTTP client’s programs in python. The implemented python scripts execute mostly on the client-side and show the results of the server’s reply in this guide. So, let’s get started with our first example in Spyder 3.

Example 01:

Let’s try our first example to see how the simple request program works in python. This program will not be about the HTTP client but the simple request to a specified URL on the network. Firstly, you need to import the “requests” module to your code top. We have to use the “requests” module “get()” method to fetch all the data on the google server in a text form. So, we have to mention the google URL in the “get” function parameter. All the text data will be stored in the “res” variable declared and initialized in the second line of code. The print statement uses the “res” variable to display it on the shell after converting it to the “text” form.

import requests

res = requests.get('https://google.com')

print(res.text)[:200]

After running this code using the “run” button of Spyder3, we have got all the data of the “google” specified URL stored in the variable “res.” Thus, the print statement uses the “res” variable in the console to display the text format data on the Spyder3 console, as shown below.

Example 02:

Let’s take a look at another example of an HTTP client. Within our first example, we glanced at making a simple request to a network server. Now, we will be looking at making an HTTP client connection. For this, you need to import the “HTTP.client” module of python first at the start of your python code. After this, the HTTP.client module is used to call the “HTTPConnection()” function to make a connection with a specified URL. The Google URL has been used here.

You have to mention the port number you have tried to connect, i.e., 80. You also have to mention the timeout for this HTTP connection. Here, we have given the “timeout” variable a value of 10 seconds. Connection response will be saved to the connection object named “con,” This variable data will be displayed on the console using the “con” variable within the print clause.

import http.client

con = http.client.HTTPConnection('http://google.com', 80, timeout=10)

print(con)

Let’s run the three-line python code within Spyder 3 using its “run” button at the middle of the tool. After running this code, we have got the output shown below in the console of Spyder 3. The connection has been established to the specified google URL using port 80 and with the specified timeout for this connection.

Example 03:

Let’s try another example of making an HTTP client program in python to create a connection. Within the above example, we have looked at how to create a connection with the help of an HTTP client module and how to get the web server’s response. So, you need to import the “HTTP.client” module at the first line using the keyword “import.” The “HTTP.client” module will be further utilized to make a connection with the URL of “journaldev” using the “HTTPConnection() function of it. The connection object “con” has been declared to save the HTTP client connection.

Now, the same connection object “con” has been utilized to call the “request” function to specify the type of request we have to do now, i.e., “GET” means to get a response. On the next line, we have been using the same “con” object to call the “getresponse()” function of http.client module. It is used to get the answer from the specified URL and save it to the “resp” variable. From the response data, the print statement will only choose to display the response status and reason for that particular status in a normal text format using the resp.status and resp.reason built-in. In last, the connection should be closed by calling the “close()” function of the HTTP.client module via the connection object “con.”

import http.client

con = http.client.HTTPConnection("www.journaldev.com")

con.request("GET", "/")

resp = con.getresponse()

print("Status: {} and reason: {}".format(resp.status, resp.reason))

con.close()

We have got the below output from running this code for 6 lines to make an HTTP connection. The output shows the Status is 301 and the reason for this status is that the particular server data is moved permanently.

Conclusion:

This article is all about how to create an HTTP client connection in python using the http.client module. We have also discussed how to make a simple connection to a web server using the simple “request” module in the python tool.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content