From cd696a6c3f8f1f2da283577d49b8310529f41308 Mon Sep 17 00:00:00 2001 From: earonesty Date: Wed, 22 Apr 2020 09:42:39 -0400 Subject: [PATCH 1/2] Warn user once about missing timeouts Not sure if this is the best way to do it, but some sort of warning that "hanging forever" is a possibility is probably important - given that this lib is for "humans". --- requests/sessions.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/requests/sessions.py b/requests/sessions.py index 2845880bf4..0c17fdec39 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -358,6 +358,9 @@ class Session(SessionRedirectMixin): 'cert', 'adapters', 'stream', 'trust_env', 'max_redirects', ] + + # whether the user has been warned about making requests without packet timeouts + __timeout_warned = False def __init__(self): @@ -413,7 +416,7 @@ def __init__(self): self.adapters = OrderedDict() self.mount('https://', HTTPAdapter()) self.mount('http://', HTTPAdapter()) - + def __enter__(self): return self @@ -628,6 +631,11 @@ def send(self, request, **kwargs): if isinstance(request, Request): raise ValueError('You can only send PreparedRequests.') + if "timeout" not in kwargs: + if not Session.__timeout_warned: + print("WARNING: timeout missing from requests call, can hang forever") + Session.__timeout_warned = True + # Set up variables needed for resolve_redirects and dispatching of hooks allow_redirects = kwargs.pop('allow_redirects', True) stream = kwargs.get('stream') From 8e60cbbd157f7389e8bf4701f23552b06c5be147 Mon Sep 17 00:00:00 2001 From: earonesty Date: Wed, 22 Apr 2020 15:00:43 -0400 Subject: [PATCH 2/2] Update sessions.py --- requests/sessions.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index 0c17fdec39..ab6e24e05a 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -10,6 +10,7 @@ import os import sys import time +import warnings from datetime import timedelta from collections import OrderedDict @@ -22,7 +23,8 @@ from ._internal_utils import to_native_string from .utils import to_key_val_list, default_headers, DEFAULT_PORTS from .exceptions import ( - TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError) + TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError, + RequestsWarning) from .structures import CaseInsensitiveDict from .adapters import HTTPAdapter @@ -633,7 +635,8 @@ def send(self, request, **kwargs): if "timeout" not in kwargs: if not Session.__timeout_warned: - print("WARNING: timeout missing from requests call, can hang forever") + warnings.warn("Timeout missing from requests call, can hang forever", + RequestsWarning) Session.__timeout_warned = True # Set up variables needed for resolve_redirects and dispatching of hooks