Skip to content

Instantly share code, notes, and snippets.

@smatting
Created May 23, 2025 07:59
Show Gist options
  • Select an option

  • Save smatting/5345171efd981e1f768bc673fcc0cefd to your computer and use it in GitHub Desktop.

Select an option

Save smatting/5345171efd981e1f768bc673fcc0cefd to your computer and use it in GitHub Desktop.
import httpx
FOO_URL = "https://google.com"
BAR_URL = "https://google.com"
def variant_shared_connection():
"""Reuse a single HTTP/2 connection for both host‑names."""
with httpx.Client(http2=True, timeout=10.0) as client:
resp_foo = client.get(FOO_URL)
print(f"[HTTP/2 Shared] GET {FOO_URL} -> {resp_foo.status_code}")
# Any cookies set by foo.com will be reused automatically
print("Cookies after first request:", client.cookies)
resp_bar = client.get(BAR_URL)
print(f"[HTTP/2 Shared] GET {BAR_URL} -> {resp_bar.status_code}")
def variant_separate_connections():
"""Open two independent HTTP/2 connections (one per request)."""
with httpx.Client(http2=True, timeout=10.0) as client1:
resp_foo = client1.get(FOO_URL)
print(f"[HTTP/2 Separate] GET {FOO_URL} -> {resp_foo.status_code}")
# New TCP/TLS handshake ➜ separate connection ➜ fresh SNI seen by LB
with httpx.Client(http2=True, timeout=10.0) as client2:
resp_bar = client2.get(BAR_URL)
print(f"[HTTP/2 Separate] GET {BAR_URL} -> {resp_bar.status_code}")
if __name__ == "__main__":
print("Running shared HTTP/2 variant...\n")
variant_shared_connection()
print("\nRunning separate HTTP/2 variant...\n")
variant_separate_connections()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment