Sending Keep-Alive Connection Request [Python Code]

To make a Keep-Alive request, specify a "Connection: keep-alive" HTTP header in the request. The Keep-Alive connection means the server won't close the connection after fulfilling the request. In HTTP 1.1, all connections are considered persistent unless declared otherwise. The servers inform the clients about persistent connections by including the "Connection: keep-alive" header in the server response. In this Keep-Alive Connection Example, we send Connection and Keep-Alive headers to the ReqBin echo URL. Click Send to execute the Keep-Alive Connection example online and see the results. The Python code was automatically generated for the Keep Alive Connection example.
Sending Keep-Alive Connection Request [Python Code] Send
GET /echo HTTP/1.1
Connection: keep-alive
Keep-Alive: timeout=5, max=100

Updated: Viewed: 65665 times
Python code for Keep Alive Connection example

Python code for Keep Alive Connection Example

This Python code snippet was generated automatically for the Keep Alive Connection example.
<< Back to the Keep Alive Connection example

What is HTTP?

Hypertext Transfer Protocol (HTTP) is a data transfer protocol that enables the communication between HTTP clients and servers. HTTP is based on a "request-response" architecture between a client and the server. The client makes a connection and initiates a request. The server receives the request, executes it, and sends the result to the client. Each HTTP message consists of a request line (status line for a response), HTTP headers, and a message body.

What is Connection header?

The Connection header that allows a single TCP connection to remain open for multiple HTTP requests/responses. Persistent connections allow sending multiple requests without opening a new connection for every single transaction. The connections are persistent by default for HTTP/1.1 clients. For HTTP/1.0 connections, you must pass the "Connection: keep-alive" request header to indicate that you explicitly want persistent connection.

When the keep-alive option is disabled and a user visits your site, the browser needs to create a new connection every time it needs to load a resource from the web page (images, JavaScripts, and CSS stylesheets). If your page contains about 50 resources, then the browser will have to establish a connection 50 times, which will significantly increase the page load time. Keep-alive connections allow all the resources of a web page to be served over a single connection, and as a result, the page is rendered much faster.

Connection Header Example
Connection: keep-alive

What is Keep-Alive header?

The Keep-Alive header allows you to set persistent connection options such as connection timeout and the maximum number of requests in a single connection.

Keep-Alive header Example
Keep-Alive: timeout=5, max=100

  • timeout: sets the maximum timeout to 5 seconds
  • max: sets the maximum number of requests per 100

How to make a Keep-Alive request?

The following is an example of sending a request to establish a persistent connection to the server using the Connection and Keep-Alive headers for the HTTP/1.0 protocol:

Keep-Alive Connection Example
GET /echo HTTP/1.0
Connection: keep-alive
Keep-Alive: timeout=5, max=100

Persistent connection in the HTTP versions

The table shows the difference between persistent connections in HTTP versions.

HTTP version Description
HTTP/1.0 In HTTP/1.0, connections are not considered persistent unless the client sends a Connection: keep-alive header with the request. When the server accepts a persistent connection request, it sends a Connection: keep-alive header back to the client in response. After that, the connection remains open, and the client sends other requests using the same connection. The connection will remain open until the client or server drops the connection.
HTTP/1.1 In HTTP/1.1, all connections are considered persistent unless otherwise specified and do not require additional Connection: keep-alive headers to establish a persistent connection. For example, the default connection timeout for Apache httpd 1.3 and 2.0 is 15 seconds and only 5 seconds for Apache httpd 2.2 and above. The advantage of a short timeout is to quickly deliver multiple web page components without using resources for too long running multiple server processes or threads.
HTTP/2 Connection and Keep-Alive headers are not allowed in HTTP/2. Chrome and Firefox ignore these headers in HTTP/2 responses, but Safari will not load the web page if the server returns these headers in error.

What are the benefits of a Keep-Alive connection?

Persistent HTTP connections reduce the time to serve requests by reducing the number of TCP and SSL/TLS connection requests. This results in shorter round-trip times (RTT). To establish TCP connections, the client must first exchange SYN and ACK packets between the client and server before transferring data. When a Keep-Alive connection is used, this means that there is no need to repeat this process with every new request, as there is already an active connection to the server. This leads to:

  1. Conserving network resources: Using one connection for multiple requests reduces the load on network resources.
  2. Reduced network congestion: Fewer TCP connections between your servers and clients can result in less network congestion.
  3. Reduced Latency: Reducing the number of 3-way handshakes can result in less site latency. This is especially true for SSL/TLS connections, which require additional round-trip cycles to encrypt and validate connections.

See also

Generate code snippets for Python and other programming languages

Convert your Keep Alive Connection request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the Python code generator.