3030import logging
3131import socket
3232import ssl
33+ import sys
34+ import time
3335from base64 import b64encode
3436from contextlib import contextmanager
3537from datetime import datetime
@@ -453,6 +455,11 @@ class Context(object):
453455 :type splunkToken: ``string``
454456 :param headers: List of extra HTTP headers to send (optional).
455457 :type headers: ``list`` of 2-tuples.
458+ :param retires: Number of retries for each HTTP connection (optional, the default is 0).
459+ NOTE THAT THIS MAY INCREASE THE NUMBER OF ROUND TRIP CONNECTIONS TO THE SPLUNK SERVER.
460+ :type retries: ``int``
461+ :param retryBackoff: How long to wait between connection attempts if `retries` > 0 (optional, defaults to 10s).
462+ :type retryBackoff: ``int`` (in seconds)
456463 :param handler: The HTTP request handler (optional).
457464 :returns: A ``Context`` instance.
458465
@@ -470,7 +477,8 @@ class Context(object):
470477 """
471478 def __init__ (self , handler = None , ** kwargs ):
472479 self .http = HttpLib (handler , kwargs .get ("verify" , False ), key_file = kwargs .get ("key_file" ),
473- cert_file = kwargs .get ("cert_file" ), context = kwargs .get ("context" )) # Default to False for backward compat
480+ cert_file = kwargs .get ("cert_file" ), context = kwargs .get ("context" ), # Default to False for backward compat
481+ retries = kwargs .get ("retries" , 0 ), retryBackoff = kwargs .get ("retryBackoff" , 10 ))
474482 self .token = kwargs .get ("token" , _NoAuthenticationToken )
475483 if self .token is None : # In case someone explicitly passes token=None
476484 self .token = _NoAuthenticationToken
@@ -1157,12 +1165,14 @@ class HttpLib(object):
11571165
11581166 If using the default handler, SSL verification can be disabled by passing verify=False.
11591167 """
1160- def __init__ (self , custom_handler = None , verify = False , key_file = None , cert_file = None , context = None ):
1168+ def __init__ (self , custom_handler = None , verify = False , key_file = None , cert_file = None , context = None , retries = 0 , retryBackoff = 10 ):
11611169 if custom_handler is None :
11621170 self .handler = handler (verify = verify , key_file = key_file , cert_file = cert_file , context = context )
11631171 else :
11641172 self .handler = custom_handler
11651173 self ._cookies = {}
1174+ self .retries = retries
1175+ self .retryBackoff = retryBackoff
11661176
11671177 def delete (self , url , headers = None , ** kwargs ):
11681178 """Sends a DELETE request to a URL.
@@ -1276,7 +1286,16 @@ def request(self, url, message, **kwargs):
12761286 its structure).
12771287 :rtype: ``dict``
12781288 """
1279- response = self .handler (url , message , ** kwargs )
1289+ while True :
1290+ try :
1291+ response = self .handler (url , message , ** kwargs )
1292+ break
1293+ except Exception :
1294+ if self .retries <= 0 :
1295+ raise
1296+ else :
1297+ time .sleep (self .retryBackoff )
1298+ self .retries -= 1
12801299 response = record (response )
12811300 if 400 <= response .status :
12821301 raise HTTPError (response )
0 commit comments