Method Name:
HTTPConnection
Method Signature:
HTTPConnection(host, port=None, [timeout,] source_address=None, block_size=8192)
Parameters:
host – The IP address of the HTTP server.
port – The Port number at which the HTTP server is listening for incoming requests.
timeout – Number of seconds to wait before timing out the connection request. This is an optional parameter.
source_address – The IP address and the port number of the HTTP client separated by a colon. The default value is None.
bock_size – Block size in which the data to be transferred. The default block size is 8192.
Return Value:
An instance of the class HTTPConnection.
Overview:
- Upon calling the instantiation function HTTPConnection, a HTTP connection is established with the specified HTTP server on the given port.
- The HTTPConnection object can be used for performing HTTP operations like sending a HTTP request - GET, POST, PUT and others, setting up a HTTP tunnel to connect through a proxy server and getting the response for the HTTP request sent.
Example:
# Example Python program that instantiates a HTTPConnection # and sends a PUT request import http.client as hc
httpMethod = "PUT"; url = "/put";
# Instantiate a HTTPConnection object connection = hc.HTTPConnection("httpbin.org");
# Send a PUT request connection.request(httpMethod, url, body="<h1>Hello World</h1>");
# Get the HTTP response httpResponse = connection.getresponse();
# Read the HTTP response response = httpResponse.read();
# Print the HTTP response print(response); |
Output:
b'{\n "args": {}, \n "data": "<h1>Hello World</h1>", \n "files": {}, \n "form": {}, \n "headers": {\n "Accept-Encoding": "identity", \n "Content-Length": "20", \n "Host": "httpbin.org"\n }, \n "json": null, \n "origin": "171.76.118.27, 171.76.118.27", \n "url": "https://httpbin.org/put"\n}\n' |