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:
See: http://pycurl.sourceforge.net/doc/curlobject.html
That being said, the
curl -doption 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:
Also, see the
urllib.urlopenfunction. 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. 🙂