RSS

Splunkd request using urrlib2, splunk-python-sdk

01 Feb

Splunk guys are so cool they put together a splunk python sdk. It comes stock standard with examples, explanations, documentation, etc…

So I’m all ready to play along and none of the examples work(for me at least).

When making a request to splunkd(the service interface) you need to be authenticated. That makes perfect sense. The logic the example uses doesn’t work though:

import httplib
import urllib
from xml.etree import ElementTree

HOST = “localhost”
PORT = 8089
USERNAME = “admin”
PASSWORD = “changeme”

# Present credentials to Splunk and retrieve the session key
connection = httplib.HTTPSConnection(HOST, PORT)
body = urllib.urlencode({‘username’: USERNAME, ‘password’: PASSWORD})
headers = {
‘Content-Type’: “application/x-www-form-urlencoded”,
‘Content-Length’: str(len(body)),
‘Host’: HOST,
‘User-Agent’: “a.py/1.0”,
‘Accept’: “*/*”
}
try:
connection.request(“POST”, “/services/auth/login”, body, headers)
response = connection.getresponse()
finally:
connection.close()

That bombs out immediately. Wiggled and jiggled some code and it still nothing. I then tried the very simple :

params = urllib.urlencode({‘username’:USERNAME, ‘password’:PASSWORD})

resp = urllib2.urlopen(url, params)

print resp.read()

The response is a session key.

Works like a charm!!! I’m still not sure why the first example didn’t though. I have never used httplib and don’t intend to. But I shall investigate!!!

urllib and urllib2 have always been awesome.

Simplicity is priceless.

 

 

Advertisements
 
2 Comments

Posted by on February 1, 2012 in Uncategorized

 

Tags: , , , , , ,

2 responses to “Splunkd request using urrlib2, splunk-python-sdk

  1. Joshua Rodman

    August 1, 2014 at 4:29 am

    Thank goodness for you. I just spent several hours trying to get python to talk to splunkd with totally weird results (empty body) for a majority of cases.

    Can’t say I agree about urllib1/2 though. Pythonic would be more like

    response = http.post(url, auth=(user, password))

     
    • kurtcampher

      October 3, 2014 at 10:49 am

      Glad it helped.

       

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: