简体   繁体   English

Python等同于PHP curl请求

[英]Python equivalent of PHP curl request

I'm using Rauth for my queries to the Beatport API. 我正在使用Rauth查询Beatport API。 I'm using this working example in PHP as reference. 在PHP中使用此工作示例作为参考。

This PHP code below uses a request token to log in and authenticate for 3-legged auth 下面的PHP代码使用请求令牌登录并进行三足身份验证

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? 如何在Python中实现? I think I need to use urllib, but I can't understand the PHP code. 我想我需要使用urllib,但我听不懂PHP代码。

use pycurl it is a python implementation of lib curl. 使用pycurl是lib curl的python实现。

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 您可以在此处找到更多示例和文档http://pycurl.sourceforge.net/doc/curlobject.html

Why not just use requests? 为什么不只使用请求? http://www.python-requests.org/en/latest/ http://www.python-requests.org/en/latest/

This library should be able to do what this php code is doing easily. 该库应该能够轻松地执行此php代码。

You should use urlopen to issue requests. 您应该使用urlopen发出请求。

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 在此处阅读更多信息: http : //docs.python.org/2/library/urllib2.html

Google urlopen code examples, and you will find what you need. Google urlopen代码示例,您将找到所需的内容。

I wouldn't bother trying to convert PHP code into python, but instead just search for a Python way of getting this answer. 我不会费心尝试将PHP代码转换为python,而只是寻找获得此答案的Python方法。 A little googling and it looks like this might be a duplicate: 稍作谷歌搜索,看起来可能是重复的:

Oauth client initialization in python for tumblr API using Python-oauth2 使用python-oauth2在tumblr API中的python中的Oauth客户端初始化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM