Splunk Search

Splunk Search results as CSV- python

poorni_p
Explorer

I am trying to get the results as CSV file with the help of this page https://www.splunk.com/blog/2011/08/02/splunk-rest-api-is-easy-to-use.html. i modified few things and successfully getting session id and SID, but getting the below error for the "Get the search status" query.

code:

    servicesSearchStatusSTR='/services/search/jobs/%s/' %sid
    isNotDone = True
    myhttp.add_credentials('username','pwd')
    while isNotDone:
        searchStatus = httplib2.Http(disable_ssl_certificate_validation=True).request(baseurl + servicesSearchStatusSTR,'GET', headers={'Authorization':'Splunk %s' % sessionkey},  body=urllib.parse.urlencode({'search':searchQuery}))[1]                                                                   
        isDoneStatus = re.compile('isDone">(0|1)')
        print(isDoneStatus)
        isDoneStatus =isDoneStatus.search(searchStatus).groups()[0]\
        if(isDoneStatus == '1'):
                     isNotDone = False

print("%s", isDoneStatus)

Error:
Traceback (most recent call last):
File "sample.py", line 46, in
isDoneStatus =isDoneStatus.search(searchStatus).groups()[0]\
TypeError: cannot use a string pattern on a bytes-like object

Please help me to resolve this error.

0 Karma
1 Solution

renjith_nair
SplunkTrust
SplunkTrust

@poorni_p,

searchStatus is an html response (a byte like object) and you need to convert that to string before doing a regex search.

Try adding this to your code just below the searchStatus(line 5 in your code snippet)

    searchStatus = searchStatus.decode('utf-8')
Happy Splunking!

View solution in original post

0 Karma

eavent_splunk
Splunk Employee
Splunk Employee

I'm not a Python expert, but from my testing on Python 2.7 I think you might just have to remove the backslash at the end of this line:

 isDoneStatus =isDoneStatus.search(searchStatus).groups()[0]\
0 Karma

renjith_nair
SplunkTrust
SplunkTrust

@poorni_p,

searchStatus is an html response (a byte like object) and you need to convert that to string before doing a regex search.

Try adding this to your code just below the searchStatus(line 5 in your code snippet)

    searchStatus = searchStatus.decode('utf-8')
Happy Splunking!
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 ...