I want to make a curl call with a URL that has multiple parameters. I have listed the code below. Is there an equivalent option for "curl -d @filter" for instance or do I have URL encode the parameters?
SER = foobar
PASS = XXX
STREAM_URL = "http://status.dummy.com/status.json?userId=12&page=1"
class Client:
def __init__(self):
self.buffer = ""
self.conn = pycurl.Curl()
self.conn.setopt(pycurl.USERPWD, "%s:%s" % (USER,PASS))
self.conn.setopt(pycurl.URL, STREAM_URL)
self.conn.setopt(pycurl.WRITEFUNCTION, self.on_receive)
self.conn.perform()
def on_receive(self,data):
self.buffer += data
Pycurl is a pretty thin wrapper for libcurl. If you can do it with libcurl, you can do it with pycurl. (Mostly.)
For instance:
pycurl.setopt corresponds to curl_easy_setopt in libcurl, where option is specified with the CURLOPT_* constants in libcurl, except that the CURLOPT_ prefix has been removed.
See: http://pycurl.sourceforge.net/doc/curlobject.html
That being said, the curl -d
option is for sending HTTP POST requests... not the GET style your example shows.
libcurl does expect that urls it recives already be URL encoded. Just use http://docs.python.org/library/urllib.html if needed.
The sample URL in your question already has 2 parameters (userId and page).
In general the format is: URL followed by a 'question mark', followed by name=value pairs joined by an ampersand symbol. If the name or value contain special chars, you will need to percent-encoded them.
Just use the urlencode function:
>>> import urllib
>>> params = [('name1','value1'), ('name2','value2 with spaces & stuff')]
>>> pairs = urllib.urlencode(params)
>>> fullurl = 'http://status.dummy.com/status.json' + '?' + pairs
>>> print fullurl
http://status.dummy.com/status.json?name1=value1&name2=value2+with+spaces+%26+stuff
>>>
Also, see the urllib.urlopen
function. Perhaps you do not need curl at all? (But I do not know your application...)
Hope this helps. If so, mark answered and let me know. :-)