Splunk Dev

Using splunk-sdk-python 1.6.5, why am I getting the following error?: "splunklib.binding.HTTPError cannot be found"

organus
Explorer

I installed splunk-sdk-python version 1.6.5 using pip on Mac OS 10.11.6 and have had moderate success cannibalizing example code from the kvstore.py. But now, I've run into an issue.

I expect to throw an error when I use "query_by_id" with a nonexistent key. Unfortunately, my stack trace states that the error object called "splunklib.binding.HTTPError" cannot be found. I can see the object being defined in the "binding.py" file.

I have lengthy experience coding in Java, but I am switching to Python, so I could be making a rookie mistake without knowing it. Does anyone see the problem?

Thanks.

This is the error:

Traceback (most recent call last):
  File "./dc2kv.py", line 108, in main
    kvRecord = json.dumps(collection.data.query_by_id(recID))
  File "/Users/dcrocker/anaconda3/lib/python3.6/site-packages/splunklib/client.py", line 3642, in query_by_id
    return json.loads(self._get(UrlEncoded(str(id))).body.read().decode('utf-8'))
  File "/Users/dcrocker/anaconda3/lib/python3.6/site-packages/splunklib/client.py", line 3612, in _get
    return self.service.get(self.path + url, owner=self.owner, app=self.app, sharing=self.sharing, **kwargs)
  File "/Users/dcrocker/anaconda3/lib/python3.6/site-packages/splunklib/binding.py", line 289, in wrapper
    return request_fun(self, *args, **kwargs)
  File "/Users/dcrocker/anaconda3/lib/python3.6/site-packages/splunklib/binding.py", line 71, in new_f
    val = f(*args, **kwargs)
  File "/Users/dcrocker/anaconda3/lib/python3.6/site-packages/splunklib/binding.py", line 669, in get
    response = self.http.get(path, self._auth_headers, **query)
  File "/Users/dcrocker/anaconda3/lib/python3.6/site-packages/splunklib/binding.py", line 1167, in get
    return self.request(url, { 'method': "GET", 'headers': headers })
  File "/Users/dcrocker/anaconda3/lib/python3.6/site-packages/splunklib/binding.py", line 1228, in request
    raise HTTPError(response)
splunklib.binding.HTTPError: HTTP 404 Not Found -- Could not find object.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./dc2kv.py", line 134, in <module>
    main()
  File "./dc2kv.py", line 109, in main
    except HTTPError as herror:
NameError: name 'HTTPError' is not defined

This is the code:

#!/usr/bin/python -tt
# dc2kv.py

import sys
import requests
import json
import splunklib.client as client
 #...
def main():
    # ...


    try:
        kvStoreConn = client.connect(**kwargs)
        print("Connected to KVStore Successfully")
        for collection in kvStoreConn.kvstore:
            print("  %s" % collection.name)

        collection = kvStoreConn.kvstore[props.get('dcCollection')]
        #print(json.dumps(collection.data.query()))
        #kvStoreConn.logout()
    except AuthenticationError:
        print("Failed to connect")
        sys.exit(1)

    for record in allRecords:
        recID = record.get('_id')
        print(recID)
        kvRecord = 'None'
        try:
            kvRecord = json.dumps(collection.data.query_by_id(recID))
        except HTTPError as herror:
            print ('Found HTTPError: ', herror)
        print('No record found.')
        if kvRecord == 'None':
            collection.insert(dumps(recID))
            print('Inserted %s', recID)
        else:
            collection.update(id = recID, data = record)
            print('Updated %s', recID)

        kvStoreConn.logout()

        sys.exit(1)
            kvStoreConn.logout()

    sys.exit(1)

if __name__ == '__main__':
  main()
0 Karma
1 Solution

organus
Explorer

Okay, I got it. I had to import the entire library, and I had to fully qualify the exception name.

BTW, there are also other problems in the code, so don't use it as a template.

#...
import splunklib
#...
        except splunklib.binding.HTTPError as herror:

View solution in original post

0 Karma

organus
Explorer

Okay, I got it. I had to import the entire library, and I had to fully qualify the exception name.

BTW, there are also other problems in the code, so don't use it as a template.

#...
import splunklib
#...
        except splunklib.binding.HTTPError as herror:
0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...