This documentation applies to the following versions of Splunk: 4.0 , 4.0.1 , 4.0.2 , 4.0.3 , 4.0.4 , 4.0.5 , 4.0.6
Here's an example of authenticating against a Splunk server and running a search query in Python.
#!/opt/splunk/bin/python -u
import urllib
import httplib2
from xml.dom import minidom
baseurl = 'https://localhost:8089'
userName = 'admin'
password = 'changeme'
searchQuery = 'sourcetype=access_common | head 5'
serverContent = httplib2.Http().request(baseurl + '/services/auth/login',
'POST', headers={}, body=urllib.urlencode({'username':userName, 'password':
password}))[1]
sessionKey =
minidom.parseString(serverContent).getElementsByTagName('sessionKey')[0].chi
ldNodes[0].nodeValue
# check if the query has the search operator
if not searchQuery.startswith('search'):
searchQuery = 'search ' + searchQuery
print httplib2.Http().request(baseurl + '/services/search/jobs','POST',
headers={'Authorization': 'Splunk %s' %
sessionKey},body=urllib.urlencode({'search': searchQuery}))[1]