Created
May 23, 2025 07:32
-
-
Save smatting/f35ec35b780f318ce45b728c3a51d558 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| SMALLSTEP_URL = "https://google.com" | |
| KEYCLOAK_URL = "https://google.com" | |
| def variant_shared_connection(): | |
| """ | |
| Uses a single HTTP connection (session) for both requests. | |
| """ | |
| with requests.Session() as session: | |
| response_foo = session.get(SMALLSTEP_URL) | |
| print(f"[Shared] GET {SMALLSTEP_URL} -> {response_foo.status_code}") | |
| response_bar = session.get(KEYCLOAK_URL) | |
| print(f"[Shared] GET {KEYCLOAK_URL} -> {response_bar.status_code}") | |
| def variant_separate_connections(): | |
| """ | |
| Uses separate HTTP connections for each request by setting Connection: close header. | |
| """ | |
| headers = {"Connection": "close"} | |
| response_foo = requests.get(SMALLSTEP_URL, headers=headers) | |
| print(f"[Separate] GET {SMALLSTEP_URL} -> {response_foo.status_code}") | |
| response_foo.close() | |
| response_bar = requests.get(KEYCLOAK_URL, headers=headers) | |
| print(f"[Separate] GET {KEYCLOAK_URL} -> {response_bar.status_code}") | |
| response_bar.close() | |
| if __name__ == "__main__": | |
| print("\nRunning variant with separate connections...\n") | |
| variant_separate_connections() | |
| print("Running variant with shared connection...") | |
| variant_shared_connection() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment