Building for the Splunk Platform

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


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'

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

 File "C:\Users\XXX\AppData\Local\Continuum\anaconda3\lib\", 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 : and

0 Karma



you're sending a string where Splunk expects bytes, a comparable situation is explained here:

I'd try to just change

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


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

Hope that helps!

0 Karma



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\ ??

0 Karma
Get Updates on the Splunk Community!

.conf23 Registration is Now Open!

Time to toss the .conf-etti 🎉 —  .conf23 registration is open!   Join us in Las Vegas July 17-20 for ...

Don't wait! Accept the Mission Possible: Splunk Adoption Challenge Now and Win ...

Attention everyone! We have exciting news to share! We are recruiting new members for the Mission Possible: ...

Unify Your SecOps with Splunk Mission Control

In today’s post, I'm excited to share some recent Splunk Mission Control innovations. With Splunk Mission ...