Short Answer
No, the standard requests
library in Python does not support HTTP/2 natively. You'll need to use alternative libraries like httpx
or requests-http2
for HTTP/2 support.
Why Requests Doesn't Support HTTP/2
The requests
library is built on top of urllib3
, which doesn't provide HTTP/2 support. The requests
library has remained focused on HTTP/1.1 and doesn't have plans to add native HTTP/2 support.
Best Solution: Using httpx
The most recommended approach is to use httpx
, which offers a similar API to requests
but with HTTP/2 support:
Installation
pip install httpx[http2]
Basic HTTP/2 Request
import httpx
# Synchronous HTTP/2 client
with httpx.Client(http2=True) as client:
response = client.get('https://www.example.com')
# Check protocol version
print(f'Protocol: {response.http_version}')
print(f'Status: {response.status_code}')
print(f'Content: {response.text}')
Async HTTP/2 Request
import asyncio
import httpx
async def make_http2_request():
async with httpx.AsyncClient(http2=True) as client:
response = await client.get('https://www.example.com')
return response
# Run the async function
response = asyncio.run(make_http2_request())
print(f'Protocol: {response.http_version}')
Advanced HTTP/2 Features
import httpx
# Configure HTTP/2 with custom settings
with httpx.Client(
http2=True,
timeout=30.0,
headers={'User-Agent': 'MyApp/1.0'}
) as client:
# Make multiple requests with connection reuse
urls = [
'https://httpbin.org/get',
'https://httpbin.org/headers',
'https://httpbin.org/user-agent'
]
for url in urls:
response = client.get(url)
print(f'{url}: {response.http_version}')
Alternative: requests-http2
If you prefer to stick closer to the requests
API, you can use requests-http2
:
pip install requests-http2
import requests_http2
# Create an HTTP/2 session
session = requests_http2.Session()
response = session.get('https://www.example.com')
print(f'Status: {response.status_code}')
print(f'Content: {response.text}')
Legacy Option: hyper (Not Recommended)
While hyper
was previously used for HTTP/2 support, it's no longer actively maintained:
# This approach is deprecated and not recommended
from hyper.contrib import HTTP20Adapter
import requests
s = requests.Session()
s.mount('https://', HTTP20Adapter())
response = s.get('https://www.example.com')
Verifying HTTP/2 Support
To test if your requests are actually using HTTP/2:
import httpx
with httpx.Client(http2=True) as client:
# Test with a known HTTP/2 server
response = client.get('https://http2.akamai.com/demo')
if response.http_version == 'HTTP/2':
print('✓ Successfully using HTTP/2')
else:
print(f'✗ Using {response.http_version}')
When to Use HTTP/2
HTTP/2 offers benefits like: - Multiplexing: Multiple requests over a single connection - Header compression: Reduced overhead - Server push: Proactive resource delivery - Binary protocol: More efficient than HTTP/1.1
Consider HTTP/2 for applications making many requests to the same server or when working with modern web services that support it.