Python |
|||||||
Python - HTTP - http.client
The http.client module in Python is a part of the Python Standard Library and provides a low-level, object-oriented interface for making HTTP requests and interacting with HTTP servers. It supports various HTTP methods such as GET, POST, PUT, DELETE, and others, as well as handling HTTP response status codes and headers.
http.client module has various components as listed below :
HTTPConnection class: This class represents a single connection to an HTTP server. You can create an instance of this class by providing the server address and an optional port number. The class provides methods for opening a connection, sending requests, and receiving responses.
HTTPSConnection class: This class is a subclass of HTTPConnection and provides support for HTTPS (HTTP over SSL/TLS). It adds encryption and server authentication to the regular HTTP connection. When creating an instance of this class, you can provide additional SSL/TLS-related parameters such as the certificate, key, and hostname verification.
HTTPStatus enumeration: This enumeration contains constants for all the HTTP response status codes, such as OK (200), BAD_REQUEST (400), and INTERNAL_SERVER_ERROR (500). These constants make it easier to work with status codes in a human-readable and self-explanatory way.
HTTPResponse class: This class represents the HTTP response returned by the server. It contains information about the status code, headers, and body of the response. You can use methods like read(), getheader(), and getheaders() to access the response data.
Methods for sending requests: The HTTPConnection and HTTPSConnection classes provide methods to send HTTP requests, such as request(), getresponse(), endheaders(), and send(). These methods allow you to specify the HTTP method, request headers, and request body.
Error handling: The http.client module defines several exception classes to handle errors that might occur during the HTTP communication process. Some of these exceptions are HTTPException, RemoteDisconnected, BadStatusLine, and IncompleteRead. These exceptions can be caught and handled in your application as needed.
Header parsing: The http.client module also provides utility functions for parsing HTTP headers, such as parse_headers(), which can parse a stream of headers into a dictionary-like object.
Examples
NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x NOTE 2 : All the examples in this page are assumed to be written/run on Windows 10 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.
< Example 01 > ========================================================
< Example 02 > ========================================================
< Example 03 > ========================================================
|
|||||||