2

Here is my Original code

import urllib, urllib2
from xml.dom import minidom

base_url = 'test.com:8000'
username = 'MyId'
password = 'passwd'
search_query = 'search error | head 10'

# Login and get the session key
request = urllib2.Request(base_url + '/servicesNS/MyId/search/auth/login', 
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 

# Perform a search
request = urllib2.Request(base_url + '/servicesNS/MyId/search/search/jobs/export', 
data = urllib.urlencode({'search': search_query,'output_mode': 'json'}),
headers = { 'Authorization': ('Splunk %s' %session_key)})
search_results = urllib2.urlopen(request)
print search_results.read()

It causes an 404 error so I decided to use Curl.

I have this curl commend

  curl -k -u myID:Passwd -d 'search="google.com | head 10"' -d "output_mode=json" https://test.com:8089/servicesNS/myId/search/search/jobs/export

I am trying to use pycurl to call Joson.

How do I convert it to pycurl?

Thank you in advance.

4
  • Show us your code - what have you tried that doesn't work? Commented May 28, 2013 at 21:05
  • Why doesn't your base_url start with 'http://'? And which of the two requests 404s? Commented May 28, 2013 at 21:50
  • 1
    your urllib2 code and the curl command do not match: different ports, different requests. ElementTree provides nicer xml api: session_key = etree.parse(server_content).findtext(".//sessionKey") Commented May 28, 2013 at 21:58
  • Have you read the pycurl tutorial, examples, and related docs on the home page? If so, what part are you stuck on? If not… go read it and come back when you can answer the previous question. Commented May 28, 2013 at 22:48

1 Answer 1

7

The best way to convert a curl command line to anything that uses libcurl (like pycurl or other bindings), is to run the command line with "--libcurl output.c" appended to it, and then you check out that created embryo as it then shows what curl had to do.

Converting that C code to pycurl code is usually trivial.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.