splunk-sdk-python icon indicating copy to clipboard operation
splunk-sdk-python copied to clipboard

Add `timeout` param

Open retzero opened this issue 1 year ago • 4 comments

Hello. Below is example code. It works fine but timed out after 2 minutes in the firewall blocked host machine. So I'd want to set shorter timeout value, eg, 30 seconds. How can I set this value in your SDK?

service = client.connect(
    scheme='http',
    host=os.getenv('SPLUNK_HOST'),
    port=int(os.getenv('SPLUNK_PORT')),
    splunkToken=os.getenv('SPLUNK_TOKEN'))

result = []
kwargs_oneshot = {"earliest_time": "-2d@d",
                  "latest_time": "-0d@d",
                  "output_mode": 'json',
                  "count": 0}
searchquery_oneshot = '''search index="my_test_index" earliest=-2d@d
           | table column_1 column_2 column_3'''

oneshotsearch_results = service.jobs.oneshot(searchquery_oneshot, **kwargs_oneshot)

Log snippet;

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 10, in splunk_search
  File "/usr/local/lib/python3.10/site-packages/splunklib/client.py", line 510, in jobs
    return Jobs(self)
  File "/usr/local/lib/python3.10/site-packages/splunklib/client.py", line 3051, in __init__
    if not service.disable_v2_api:
  File "/usr/local/lib/python3.10/site-packages/splunklib/client.py", line 714, in disable_v2_api
    if self.splunk_instance.lower() == 'cloud':
  File "/usr/local/lib/python3.10/site-packages/splunklib/client.py", line 705, in splunk_instance
    splunk_info = self.info
  File "/usr/local/lib/python3.10/site-packages/splunklib/client.py", line 479, in info
    response = self.get("/services/server/info")
  File "/usr/local/lib/python3.10/site-packages/splunklib/binding.py", line 318, in wrapper
    return request_fun(self, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/splunklib/binding.py", line 76, in new_f
    val = f(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/splunklib/binding.py", line 737, in get
    response = self.http.get(path, all_headers, **query)
  File "/usr/local/lib/python3.10/site-packages/splunklib/binding.py", line 1272, in get
    return self.request(url, {'method': "GET", 'headers': headers})
  File "/usr/local/lib/python3.10/site-packages/splunklib/binding.py", line 1334, in request
    response = self.handler(url, message, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/splunklib/binding.py", line 1488, in request
    connection.request(method, path, body, head)
  File "/usr/local/lib/python3.10/http/client.py", line 1283, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.10/http/client.py", line 1329, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.10/http/client.py", line 1278, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.10/http/client.py", line 1038, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.10/http/client.py", line 976, in send
    self.connect()
  File "/usr/local/lib/python3.10/http/client.py", line 942, in connect
    self.sock = self._create_connection(
  File "/usr/local/lib/python3.10/socket.py", line 845, in create_connection
    raise err
  File "/usr/local/lib/python3.10/socket.py", line 833, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out

retzero avatar May 07 '25 11:05 retzero

Hi,

Currently we do not have such way to specify a timeout, but it seems like the SDK already supports timeouts it in the http handler:

https://github.com/splunk/splunk-sdk-python/blob/c710f47efad51fd921003c91c51ec2b86bf2b18c/splunklib/binding.py#L1512-L1513

So it seems doable, we would only need to add and propagate that param from connect()/Service to the handler.

mateusz834 avatar Sep 11 '25 06:09 mateusz834

Actually we have a different idea for this, we accept custom handlers, so timeouts can be specified as such:

from splunklib import client
from splunklib.binding import handler

c = client.connect(
    handler=handler(timeout=5), host="localhost", port="8080", scheme="http"
)

mateusz834 avatar Sep 19 '25 06:09 mateusz834

@mateusz834 Oh. Great suggestion! Thank you so much 😄

retzero avatar Sep 19 '25 08:09 retzero

@mateusz834 maybe there's value in adding it to examples?

Ickerday avatar Sep 22 '25 15:09 Ickerday