[tor-commits] [sbws/master] requests: Add class to override Session methods

juga at torproject.org juga at torproject.org
Sat Feb 23 10:57:26 UTC 2019


commit f19b0c68e2f1022187e8d3406f2e33fe3b42b77b
Author: juga0 <juga at riseup.net>
Date:   Thu Feb 21 13:51:04 2019 +0000

    requests: Add class to override Session methods
    
    Override get and head methods to always have a timeout.
---
 sbws/util/requests.py | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/sbws/util/requests.py b/sbws/util/requests.py
index 0b1ece2..e0a6864 100644
--- a/sbws/util/requests.py
+++ b/sbws/util/requests.py
@@ -1,7 +1,20 @@
 import requests
 
 from sbws import settings
-import sbws.util.stem as stem_utils
+from sbws.util import stem as stem_utils
+
+
+class TimedSession(requests.Session):
+    """Requests Session that sends timeout in the head and get methods.
+    """
+
+    def get(self, url, **kwargs):
+        return super().get(url, timeout=getattr(self, "_timeout", None),
+                           **kwargs)
+
+    def head(self, url, **kwargs):
+        return super().head(url, timeout=getattr(self, "_timeout", None),
+                            **kwargs)
 
 
 def make_session(controller, timeout):





More information about the tor-commits mailing list