Python code like curl

diegueus9 picture diegueus9 · Oct 20, 2010 · Viewed 11.4k times · Source

in curl i do this:

curl -u email:password http://api.foursquare.com/v1/venue.json?vid=2393749

How i can do this same thing in python?

Answer

Sam Dolan picture Sam Dolan · Oct 20, 2010

Here's the equivalent in pycurl:

import pycurl
from StringIO import StringIO

response_buffer = StringIO()
curl = pycurl.Curl()

curl.setopt(curl.URL, "http://api.foursquare.com/v1/venue.json?vid=2393749")

curl.setopt(curl.USERPWD, '%s:%s' % ('youruser', 'yourpassword'))

curl.setopt(curl.WRITEFUNCTION, response_buffer.write)

curl.perform()
curl.close()

response_value = response_buffer.getvalue()