简体   繁体   中英

Python equivalent of PHP curl request

I'm using Rauth for my queries to the Beatport API. I'm using this working example in PHP as reference.

This PHP code below uses a request token to log in and authenticate for 3-legged auth

ini_set('max_execution_time', 500);
$submit = "Login";
$url = $auth_submiturl;

$curl_connection_bp = curl_init();
curl_setopt($curl_connection_bp, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_connection_bp, CURLOPT_URL, $url);
curl_setopt($curl_connection_bp, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl_connection_bp, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT6.0; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11");
curl_setopt($curl_connection_bp, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl_connection_bp, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection_bp, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl_connection_bp, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection_bp, CURLOPT_VERBOSE, false); // when true, this outputs the oauth_token and oauth_verifier value that are posted to the callback URL
curl_setopt($curl_connection_bp, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($curl_connection_bp, CURLOPT_REFERER, $curl_connection_bp);
curl_setopt($curl_connection_bp, CURLOPT_FAILONERROR, 0);
$post_string = 'oauth_token='.$request_token_info['oauth_token'] . '&username=' . $beatport_login . '&password=' . $beatport_password . '&submit=Login';
curl_setopt($curl_connection_bp, CURLOPT_POST, true);
curl_setopt($curl_connection_bp, CURLOPT_POSTFIELDS, $post_string);
$beatport_response = curl_exec($curl_connection_bp);
$beatport_response = json_decode($beatport_response);

print_r($beatport_response);

How can I achieve this in Python? I think I need to use urllib, but I can't understand the PHP code.

use pycurl it is a python implementation of lib curl.

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://www.python.org/")
c.setopt(pycurl.HTTPHEADER, ["Accept:"])
...

you can find more examples and documentation here http://pycurl.sourceforge.net/doc/curlobject.html

Why not just use requests? http://www.python-requests.org/en/latest/

This library should be able to do what this php code is doing easily.

You should use urlopen to issue requests.

A simple example:

import urllib2
result = urllib2.urlopen("http://www.google.com/")
print result.read()

Read more here: http://docs.python.org/2/library/urllib2.html

Google urlopen code examples, and you will find what you need.

I wouldn't bother trying to convert PHP code into python, but instead just search for a Python way of getting this answer. A little googling and it looks like this might be a duplicate:

Oauth client initialization in python for tumblr API using Python-oauth2

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