Splunk Dev

splunk client send error "a bytes-like object is require" with python script

Chandras11
Communicator

Hi Everyone,

I am trying to upload CSV file to a certain index and a certain sourcetype (already exist in Splunk) with a python script. I have already installed Python SDK Splunk and I am using Python 3 with anaconda

import splunklib.client as client
service = client.connect(host='localhost',port=8089,username='admin',password='Passwd')
myindex = service.indexes["Index1"]
mysocket = myindex.attach(sourcetype='ST1',host='My_PC')
file_data = ''
with open("ST1_23_Mai_2018.csv", "r") as lines:
    for line in lines:
        if line.isspace(): 
            line = ' '
            file_data += line 
            file_data += '\r\n'
    mysocket.send(file_data)
mysocket.close()

The SSL handshake is successful. However, I am getting following error through python:-

 File "C:\Users\XXX\AppData\Local\Continuum\anaconda3\lib\ssl.py", line 642, in write
    return self._sslobj.write(data)

TypeError: a bytes-like object is required, not 'str'

Does anyone have faced similar situation before. I try to check the splunk documentation at : http://dev.splunk.com/view/python-sdk/SP-CAAAEE6#add2index and http://docs.splunk.com/DocumentationStatic/PythonSDK/1.6.3/client.html#splunklib.client.Index.upload

0 Karma

xpac
SplunkTrust
SplunkTrust

Hey,

you're sending a string where Splunk expects bytes, a comparable situation is explained here:
https://stackoverflow.com/questions/33054527/python-3-5-typeerror-a-bytes-like-object-is-required-no...

I'd try to just change

 with open("ST1_23_Mai_2018.csv", "r") as lines:

to

 with open("ST1_23_Mai_2018.csv", "rb") as lines:

Hope that helps!

0 Karma

Chandras11
Communicator

Hi,

Thanks for the answer. I already tried to make it rb (read as bytes) but I didn't get any success. The error is still the same.
Do I need to change some properties in anaconda3\lib\ssl.py ??

0 Karma
Get Updates on the Splunk Community!

Join Us for Splunk University and Get Your Bootcamp Game On!

If you know, you know! Splunk University is the vibe this summer so register today for bootcamps galore ...

.conf24 | Learning Tracks for Security, Observability, Platform, and Developers!

.conf24 is taking place at The Venetian in Las Vegas from June 11 - 14. Continue reading to learn about the ...

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...