Python requests is a much appreciated library which allows you to send HTTP requests and check the answers. Even if .get() is the most important method of the module, there are many other options.
Python requestswhat exactly is it?
Python requests is a widely used third library for Send HTTP requests within the programming language. The cornerstone of this system is a clear and effective interface which greatly facilitates the integration of third -party features. The Python library requests Available a series of tools that allow HTTP requests to be sent and receive the answers. Even if the Python module requests is widely used and highly recommended, It is not part of the standard installation of Python.
Managed NextCloud of Ionos Cloud
Work as a team in your own cloud
- Data security
- Integrated collaboration tools
- Accommodation in European data centers
Installation and start -up of the Python library requests
Before installing Python requestsit is recommended to set up a virtual machine in which you can try the library without fear. For installation, use pip. Here is what the code looks like:
$ python -m pip install requests
bash
To use the library after having successfully installed it, you need this code:
Operation, syntax and methods
Like other comparable tools, Python requests Sends an HTTP request which receives an answer following a successful transmission. This answer is an object that contains all important information such as Content, coding and status. The syntax of such a request always presents itself as follows:
requests.methode(url, **kwargs)
python
Note
**kwargs represents optional methods that you can give to methods like headers,, files Or cookies.
Even if .get() is the function you will use most often, Python requests also offers other methods. Here are the main functions available:
.delete(url,** kwargs): remove a specific source..get(url, parameter=None,** kwargs): is used to request information from a server..head(url,** kwargs): perform a request to the server, but is limited to the header..patch(url, data=None,** kwargs): Optimizes performance by sending only changes to the source..post(url, data=None, json=None,** kwargs): used mainly to transmit form data to a server..put(url, data=None,** kwargs): modify an existing source or creates new data on the server..request(method, url,** kwargs): Send a request with a method specific to a given URL.
Example concerning the method .get()
The syntax of .get() is very clear, which makes python requests A very practical library. Just the method and the URL you want to access. This is indicated in quotes. Here is an example of a code of this type:
import requests
requests.get("https://exemple.com")
python
The target server sends you an answer. To illustrate this, you can also display it in a variable. In our example, it would look like this:
import requests
response = requests.get("https://exemple.com")
python
Consult the status codes
The simplest answer given to you to a request is a HTTP status code. This one tells you whether your request has led or not. The status code is made up of Three figures. There are many variants that inform you of the evolution of your request. The main categories are as follows:
- 1xx : Contains additional information.
- 2xx : attest to a successful request.
- 3xx : informs you of a redirection of your request.
- 4xx : signals an error on the customer side.
- 5xx : notify an error on the server side.
Noticed
One of the best -known status codes is « 200 – OK » (for a successful request) and « 404 – Not Found » (if the requested data have not been found on the server).
After your request via Python requestsyou can consult the status with .status_code. To do this, use the following code:
response.status_code
python
If your request has succeeded, you will receive the following message:
The following code is useful for better presentation:
if response.status_code == 200:
print("La requête a abouti.")
elif response.status_code == 404:
print("La requête n’a pas abouti.")
python
Consult the header with Python requests
The header of an HTTP response contains a lot of useful information. Among other things, you will find there Type of data sent, a chatting period and other indications. The Python library requests also allows you to easily display the header. To do this, first perform the function .get()Then .headers ::
import requests
response = requests.get("https://exemple.com")
response.headers
python
At the output, you get an object that contains the values of the header. You can also consult them individually by specifying the command. If you want, for example, to learn more about the type of data, here is the right order:
response.headers["content-type"]
python
Access the payload
Data package sent between the server and the customer is also known as « payload » (payload). It is in the body of the answer. response Allows access to the information there. To obtain an byte representation, the following investigation is indicated:
import requests
response = requests.get("https://exemple.com")
response.content
type(response.content)
python
If you want to convert the information collected into a character string (string), use the following code:
response.text
type(response.text)
python
Python requests Extracts the encoding from the header. You can also ask the system to use another method. This code is an example:
response.encoding = "utf-8"
response.text
python
Check
When you send a request, it is prepared by Python requests to avoid errors. By doing so, The header is checkedamong others. You can inspect the details of the request sent using the function .request. To do this, use the following code:
import requests
response = requests.post("https://exemple.com", json={"key": "value"})
print(response.request.headers["content-type"])
print(response.request.url)
print(response.request.body)
python
You get information on the payload, the header, the URL and more.
Authenticity verification via the Python module requests
The parameter auth is used in Python requests To send authentication to the server. This can make an authenticity check and control, thus, with whom it really interacts. To use this verification, apply a code like this:
import requests
from requests.auth import HTTPBasicAuth
response = requests.get("https://votre_page.com/basic-auth/user/passwd",
auth=HTTPBasicAuth("user", "passwd"))
print(response.status_code)
print(response.request.headers["Authorization"])
python
Use an SSL certificate
In principle, the use of an SSL certificate is useful for avoiding data loss and unwanted intrusions. Python requests offers this default option. If you want however prevent mutual verification for a requestyou can deactivate SSL thanks to the following code:
import requests
requests.get("https://exemple.com", verify=False)
python
You will then receive an explicit warning message.
Note
Disable SSL verification can Compromise the safety of your application By making it vulnerable to cyber attacks. Use this option only for tests or in development environments where security is not critical.
Trigger a timeout with python requests
Python requests allows, among other things, to limit Maximum time for an answer. By default, the customer awaits a response without restriction. In the absence of it, requests can accumulate and perform. The parameter timeout allows you to avoid this problem. In the following code, we indicate to Python requests to interrupt the request after two seconds:
requests.get("https://exemple.com", timeout=2)
python
Advice
Deploy your website or application directly via GitHub: with Deploy Now from Ionos, you have the optimal solution for single page applications as for static websites. Choose the price that best suits your needs and enjoy a faster installation, optimized workflows and a secure design!

