将CURL php脚本转换为python代码

时间:2015-03-13 15:52:25

标签: python http post

我需要转换这个php函数,它使用CURL登录到Web服务器,然后请求png资源。

function GraphImageById ($graphid, $period = 3600, $width, $height) { global $z_server, $z_user, $z_pass, $z_tmp_cookies, $z_url_index, $z_url_graph, $z_url_api, $z_img_path,   $z_login_data;
    // file names
    $filename_cookie = $z_tmp_cookies ."zabbix_cookie_" .$graphid .".txt";
    $image_name = $z_img_path ."zabbix_graph_" .$graphid .".png";

    //setup curl
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $z_url_index);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $z_login_data);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $filename_cookie);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $filename_cookie);
    // login - 1st curl exec.
    curl_exec($ch);
    // get graph
    curl_setopt($ch, CURLOPT_URL, $z_url_graph ."?graphid=" .$graphid ."&width=" .$width ."&height=" .$height ."&period=" .$period);
    // set response header
    header("Content-type: image/png");
    $output = curl_exec($ch); // - 2nd curl exec.
    curl_close($ch);
    unlink($filename_cookie);
    return $output;

}

我可以使用哪个python库?有什么建议? 感谢。

1 个答案:

答案 0 :(得分:1)

可能pycurl(python的cURL lib绑定)是你想要的(https://pypi.python.org/pypi/pycurl),但我很确定你也可以用httplib / httplib2来解决这个问题。