简体   繁体   中英

how to convert curl command line to pycurl code

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM