Python Network Programming HTTP Request

Python Network Programming: HTTP Requests

HTTP, or the Hypertext Transfer Protocol, operates in a client-server model. Typically, a web browser is the client, and the computer hosting the website is the server. In Python, we use the requests module to create HTTP requests. It’s a very powerful module that handles many aspects of HTTP communication beyond simple request and response data. It can handle authentication, compression/decompression, chunked requests, and more.

An HTTP client sends an HTTP request to a server in the form of a request message. The request message has the following format:

  • A request line
  • Zero or more header (general | request | entity) fields, followed by a CRLF
  • A blank line (i.e., nothing before the CRLF) indicates the end of the header fields.
  • Optionally, a message body

The following sections explain each entity used in an HTTP request message.

Request-Line

The Request-Line begins with a method tag, followed by the Request-URI and protocol version, and ends with a CRLF. These elements are separated by the space character SP.

Request-Line = Method SP Request-URI SP HTTP-Version CRLF

Let’s discuss each of the components mentioned in the request line.

Request Method

The request method indicates the method to be performed on the resource identified by the given Request-URI. The method is case-sensitive and should always be mentioned in uppercase letters. The following table lists all the methods supported in HTTP/1.1.

S.N. Method and Description
1 GET The GET method is used to retrieve information from a given server using a given URI. Requests using GET should only retrieve data and should not otherwise affect it.
2 HEAD Same as GET, but only transmits the status line and headers.
3 POST The POST request is used to send data to a server, such as customer information or file uploads, using an HTML form.
4 PUT Replaces all current representations of the target resource with the uploaded content.
5 DELETE Deletes all current representations of the target resource given by the URI.
6 CONNECT Establishes a tunnel to the server identified by the given URI.
7 OPTIONS Describes the communication options for the target resource.
8 TRACE Performs a message loopback test along with the path to the target resource.

Request-URI

A Request-URI is a Uniform Resource Identifier (URI) that identifies the resource being requested by an application. The following are the most common forms for specifying a URI.

Request-URI = "*" | absoluteURI | abs_path | authority
S.N. Method and Description
1 The asterisk ***** is used to indicate that the HTTP request does not apply to a specific resource, but rather to the server itself. It is permitted only when the method being used is not necessarily applicable to a specific resource. For example: OPTIONS * HTTP/1.1
2 Absolute URIs are used when making HTTP requests to a proxy. The proxy is asked to forward the request or service from a valid cache and return the response. For example: GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1
3 The most common form of the Request-URI is used to identify a resource on an origin server or gateway. For example, a client wishing to retrieve a resource directly from the origin server would establish a TCP connection to port 80 of the host “www.w3.org” and send the following lines: GET /pub/WWW/TheProject.html HTTP/1.1 Host: www.w3.org Note that the absolute path cannot be empty; the “/” (server root) must be present if it was not present in the original URI.

Using Python Requests

We will use the requests module to learn about http requests.

pip install requests

In the following example, we see a simple GET request and print the response. We choose to print only the first 300 characters.

# How to make http request
import requests as req
r = req.get('http://www.tutorialspoint.com/python/')
print(r.text)[0:300]

When we run the above program, we get the following output −

<!DOCTYPE html>
<!--[if IE 8]><html class="ie ie8"> <![endif]-->
<!--[if IE 9]><html class="ie ie9"> <![endif]-->
<!--[if gt IE 9]><!--> <html> <!--<![endif]-->
<head>
<!-- Basic -->
<meta charset="utf-8">
<title>Python Tutorial</title>
<meta name="description" content="Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *