【问题标题】:Converting PHP curl_setopt() to Python Requests and to CLI curl将 PHP curl_setopt() 转换为 Python 请求和 CLI curl
【发布时间】:2016-04-14 22:04:20
【问题描述】:

我正在尝试将以下 PHP curl_setopt() 转换为 Python Requests 以及 CLI curl 中的等效项。对于 Python,如果在 Requests 中不可能,我将使用 pycurl

curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);

这是工作 PHP curl 代码:

$params = array(
    'username' => $username,
    'password' => $password
);

$params_string = json_encode($params);

$process = curl_init($url);
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($process, CURLOPT_POSTFIELDS, $params_string);

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);

$data = curl_exec ($process);

curl_close ($process);   

我不知道需要在 Python 请求 中为这两个 PHP curl_setopt(s) 设置什么。

我在 CLI curl 中尝试了以下操作,但收到 Bad Request 错误:

AUTHENTICATE_DATA="usename=${USERNAME}&password=${PASSWORD}"

AUTHENTICATE_RESPONSE=$(curl \
  -X POST \
  -H 'Content-Type: application/json' \
  --data-urlencode "${AUTHENTICATE_DATA}" \
  -v \
  ${AUTHENTICATE_URL})

echo ${AUTHENTICATE_RESPONSE}

我需要什么?

谢谢,感谢您的帮助。

【问题讨论】:

    标签: php python curl python-requests pycurl


    【解决方案1】:

    使用请求:

    import requests
    
    url = 'https://...'
    username = 'username'
    password = 'supersecret'
    
    # curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
    # PHP bashing time: Who in their right minds would create a function
    # that spews its return value to stdout by default, unless explicitly
    # instructed not to do so.
    resp = requests.post(
    
        # $process = curl_init($url);
        url,
    
        # $params = array(
        #    'username' => $username,
        #    'password' => $password
        # );
        # $params_string = json_encode($params);
        # curl_setopt($process, CURLOPT_POSTFIELDS, $params_string);
        # $headers[] = 'Content-Type: application/json';
        # curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
        json=dict(username=username, password=password),
    
        # curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
        # note: you shouldn't do this, really
        verify=False,
    
    )
    
    data = resp.text
    # or if you expect json response
    data = resp.json()
    

    【讨论】:

    • 谢谢,这很好用,并按照建议更改为 verify=True
    • 你可以完全跳过它,默认是True。你也可以用它来传递"path to a CA_BUNDLE file or directory with certificates of trusted CAs"
    • PHP 函数是 cURL API 函数的一对一映射,这就是为什么它们的行为可能不完全合理!在 PHP 中处理 HTTP 请求有更好、更现代的方法。
    • 来自@miken32 在 PHP 中有更好、更现代的方式来处理 HTTP 请求。这些更现代的方式是什么?
    • @JeffTanner streamsHTTP client
    【解决方案2】:
    curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
    

    这个选项只是说curl_exec() 的结果被传递给一个返回变量。在命令行上,默认输出到标准输出。

    curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
    

    这是一个非常糟糕的主意,会使您容易受到中间人攻击。没有任何借口可以使用它。 --insecure 是等效的命令行选项。

    另外值得一提的是,您正在传递一个 JSON 字符串作为 CURLOPT_POSTFIELDS 的参数,并且您拥有的 CLI 代码没有进行 JSON 编码。

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2017-04-12
      • 2016-01-30
      • 2020-03-18
      相关资源
      最近更新 更多