Python code for Curl Keep Alive Connection Header Example
This Python code snippet was generated automatically for the Curl Keep Alive Connection Header example.<< Back to the Curl Keep Alive Connection Header example
What is Curl?
Curl is a command-line utility available for Linux, Windows, and macOS, and that allows sending HTTP requests to the server with different parameters and methods. Curl supports over 25+ protocols, including HTTP, HTTPS, FTP, and has built-in support for HTTP Cookies, web forms, SSL, user authentication, and proxies.
What is Connection Header?
The Connection header determines whether the current TCP connection remains open after a transaction. Persistent connections allow you to send multiple requests without opening a new connection for each transaction. By default, connections are persistent for HTTP/1.1 clients. For HTTP/1.0 connections, you need to pass the "Connection: keep-alive" request header to indicate that you want a persistent connection.
Curl Connection Header Syntax
The following is the syntax of sending a Curl request with a Keep-Alive Connection Header:
What is Keep-Alive header?
The Keep-Alive header allows you to set the parameters of a persistent connection: connection timeout and the maximum number of requests in one connection.
Where:
- timeout: sets the maximum timeout to 5 seconds
- max: sets the maximum number of requests per connection to 100
How to make a Keep-Alive request with Curl?
An example of sending a Curl request with directives for establishing a persistent connection and with connection parameters:
HTTP versions support persistent connections
The table shows the difference between persistent connections in HTTP versions.
HTTP version | Description |
---|---|
HTTP/1.0 | In HTTP/1.0, connections are not persistent unless the client sends the -H "Connection: keep-alive" header. The server responds to persistent connections by sending the client the "Connection: keep-alive" header. The connection will then be open, and the client can send other requests through the same connection. Connections will remain open until they are dropped by either the client or server. |
HTTP/1.1 | HTTP/1.1 considers all connections persistent unless otherwise specified and does not require additional -H "Connection: keep-alive" headers. |
HTTP/2 | HTTP/2 does not allow connection and keep-alive headers. Chrome and Firefox ignore these headers in HTTP/2 responses, but Safari won't load a page with these headers. |