Python

Python Curl Example

cURL is a client URL. It is used by developers as a command-line tool to transfer data to the server and similarly accept the coming data from it. When we talk about cURL, its relation with the server always comes to mind because it makes us able to connect with the server by providing us with two things. One is the specific location that is in the form of a URL, and the second one is the data to be sent. cURL is good in supporting many protocols like HTTP and HTTPS. It is not framework-dependent as it is executable on almost every platform. This feature of curl makes it the perfect choice for testing the communication and network information from any device from a local server.

The basic example of curl is:

# Curl http://sample.com.

The curl command contains the URL that is used to fetch the data from the particular address. As a result, we will get the HTML source for example.com. It is known to be a request tool that creates network requests so that it allows the transfer of data through the network.

Why Do We Use Curl?

A curl is a portable tool. It is independent of the operating systems and is compatible with almost every connected device. This tool is not only known for the fetching of address and data but also useful in testing the endpoints, to which it is connected, to check whether they are properly working or not. Errors are easily identified by using curl and have good error loggings. It can provide all the details or history of each item that has been sent or received. That information is useful in the debugging process.

Curl Command in Python

Pycurl is very fast and works faster than the requests that is the library of python for the HTTP requests. It has multiprotocol support and is capable of containing the sockets for the network operations to be held.

Curl is a UNIX command that is used to send the PUT, GET, and POST requests to a URL. For python, we use an HTTP library named “Requests”. But this library is not considered a standard module.

Curl Command Execution in Python

A curl command in Python contains many features like URL, headers, requests and the data to be sent/received. Now let us see the examples for each feature.

Examples to Implement Curl in Python

URL:

Headers:
To display the headers of any website, simply use the keyword of the header with the API key, this key is responsible for giving the information regarding the address.

Request:
To see the request sent from the client to the server, it is obtained by combining all the above-mentioned codes in which we show the URL and the headers to see the request status for the get method.

# Resp = request.get(url, headers = headers)

The request code for this URL will be 200.

Data:

The content present at that specified address is displayed through the below command. This command is added with the code that is used to obtain the status.

print (resp.content)
print (resp)

On Windows, if we use a sample URL just to see what output is obtained, go to the start search bar, and search for the CMD. Now, write the command of the URL on the CMD. We have used an example of Google here.

It will display the content from which the website is being developed. i.e., that is the HTML code.

There are many request methods used for curl. For example, requests.put(), requests.post(), requests.delete() etc. Now, we will see the syntax for each of the requests.

Call.request.get(URL)

It is used to send the request of the get() method to the URL for the server to get the information. This takes only the URL as an argument.

Call.request.post(URL, data= dict)

This function sends the request for the post() to send the information to the URL. It takes a data variable that takes a dictionary. This dictionary contains the keys.

Call.request.put(URL, data= dict)

This call also works similarly to the post function. And will also send the same values as an argument.

Call.request.delete(URL, data= dict)

It will send the delete function request to the URL containing the same arguments.

Now, we will explain two of the commonly used requests with examples.

Get Method

This method is a part of the python requests module that is used to obtain data from the URL of a website. Through the get method, it receives much information like responses. We get the response time and header as well. To implement the code, you need a python running tool. Here, we will use ‘spyder’ for this purpose. Create a file and then add data to it.

import requests
req = requests.get('http://www.linuxhint.com/')
#Page encoding
e= req.encoding
print("Encoding: ",e)
# Response code
s = req.status_code
print("Response code: ",s)
# Response Time
t = req.elapsed
print("Response Time: ",t)
t = req.headers['Content-Type']
print("Header: ",t)
z = req.text
print("\nSome text from the web page:\n",z[0:200])

The get method will take a URL. We have taken the URL of the website “Linuxhint.com” as an example. For each response, we have taken a variable that will store the answer from the URL. The response code is 200, similar to the example we have described in the previous example. Whereas the response time and the header part are also present.

Post Method

This method is used to send the data mostly through the form to a server for updating or deletion of data already uploaded. This function can directly send the data by using the URL and the value of the data parameter. We have used an example in which we post some data to the “httpbin.org” website by using a post method. This will provide us with a response.

This will take the username and a password.

Res = request.post(‘https://httpbin.org/post’, data = in_values)

This post function will take the URL and the username and password as the argument to pass to the desired URL.

The resultant value shows the information regarding Host, user-Agent, and the length of the code in the header part.

Options of cURL

There are around about 200 options for curl in each platform like UNIX, Windows, etc.

In Windows:

# Curl  - -help

This will display almost all the features of curl. And also some useful information is enclosed in the description part.

In Linux:

In the Linux Ubuntu operating system, we use manual of curl. The output of this command contains approximately similar information as obtained in the Windows.

# Curl - -manual

Some of the commonly used options are listed below.

–I: It returns only HTTPS headers. For example, if you use any URL, you need to add this option with a URL.

# Curl  -request Get ‘https:/www.google.com/ -I

This will bring the information in the header like Date, content length, etc.

-V: This is a verbose option; this shows everything that happens when we run the curl command. We also obtain the information of the images that are returned from the URL.

-O: It stores the output of the particular URL in a file.

Conclusion

‘PYTHON CURL EXAMPLE’ contains the use of curl in both Windows and the Linux operating system. The basic usage and description of URL addresses are explained with simple examples. Two major methods like getting and post methods are explained in detail. Through this article, we aim to help the user in the field of curl usage in Python.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.