Deployment Architecture

How to call /services/deployment/server/clients via urllib2 request

lohit
Path Finder

Hi all,

i have developed a query to check my host lastphonehoemtime information in DS as below:

|rest /services/deployment/server/clients splunk_server=local  | eval current = now() | convert ctime(lastPhoneHomeTime) as last ctime(current) as now | fields hostname ip last last now

The above query is working fine.
Now i want to call this via SDK, so i am using below code for that

ds_url = 'https://xyz:8089'
username='abc'
password='pqr'


request = urllib2.Request(ds_url + '/servicesNS/%s/search/auth/login' % (username),
data = urllib.urlencode({'username': username, 'password': password}))
server_content = urllib2.urlopen(request)

session_key = minidom.parseString(server_content.read()).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue
print "Session Key: %s" % session_key


search_query=' |rest /services/deployment/server/clients | eval current = now() | convert ctime(lastPhoneHomeTime) as last ctime(current) as now | fields hostname ip last last now'
request = urllib2.Request(ds_url + '/services/deployment/server/clients',
    data = urllib.urlencode({'search': search_query,'output_mode': 'csv'}),
    headers = { 'Authorization': ('Splunk %s' %session_key)})
search_results = urllib2.urlopen(request)
print search_results.read()

I have two questions:

  1. How should i write the Splunk query to insert in the urllib2 object. Since in the main query 'rest' should be the first parameter and when calling via SDK first parameter should be 'search' ?
    1. How to pass value of /services/deployment/server/clients in urllib2.request method as the format should be /services/<user>/<app> ?

Any help would be highly appreciated !!

Tags (2)
0 Karma

sowings_splunk
Splunk Employee
Splunk Employee

Is there some reason that you could not use the Splunk Python SDK? It handles a lot of that header stuff for you.

EDIT: Consider this page for an example of how to connect to Splunk with the Python SDK: http://dev.splunk.com/view/python-sdk/SP-CAAAEE4

Next, this page describes how to run a search:

http://dev.splunk.com/view/python-sdk/SP-CAAAEE5

With these, you shouldn't need direct calls to urllib2 methods; the SDK will handle that for you.

0 Karma

lohit
Path Finder

I do not see much examples over splunk documentation. If there are could you provide me those. however i think my questions would stand out even if i use Splunk SDK.

0 Karma
Get Updates on the Splunk Community!

Accelerating Observability as Code with the Splunk AI Assistant

We’ve seen in previous posts what Observability as Code (OaC) is and how it’s now essential for managing ...

Integrating Splunk Search API and Quarto to Create Reproducible Investigation ...

 Splunk is More Than Just the Web Console For Digital Forensics and Incident Response (DFIR) practitioners, ...

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...