Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python code like curl

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?

like image 546
diegueus9 Avatar asked Oct 19 '10 22:10

diegueus9


People also ask

What is the equivalent of Curl in Python?

In Python, cURL transfers requests and data to and from servers using PycURL. PycURL functions as an interface for the libcURL library within Python.

How do you make a Curl in Python?

Enter the Curl command, click Run to execute the command online, and check the results. With our Curl to Python Converter, you can convert almost any Curl command to Python code with just one click. Click Generate Code and select Python to convert the Curl command to Python code.

How do I get Curl request in Python?

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.

Is PycURL faster than requests?

Beyond simple fetches however PycURL exposes most of the functionality of libcurl, including: Speed - libcurl is very fast and PycURL, being a thin wrapper above libcurl, is very fast as well. PycURL was benchmarked to be several times faster than Requests.


2 Answers

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()
like image 115
Sam Dolan Avatar answered Sep 29 '22 20:09

Sam Dolan


"The problem could be that the Python libraries, per HTTP-Standard, first send an unauthenticated request, and then only if it's answered with a 401 retry, are the correct credentials sent. If the Foursquare servers don't do "totally standard authentication" then the libraries won't work.

Try using headers to do authentication:"

taked from Python urllib2 Basic Auth Problem

import urllib2
import base64

req = urllib2.Request('http://api.foursquare.com/v1/venue.json?vid=%s' % self.venue_id)
req.add_header('Authorization: Basic ',base64.b64encode('email:password'))
res = urllib2.urlopen(req)
like image 28
diegueus9 Avatar answered Sep 29 '22 19:09

diegueus9