感歎!不管神馬東西用python寫就會變得巨短!

來源:互聯網
上載者:User

        上次是wxWidget,這次又發現一個cURL。python版本分別叫做wxPython和pycurl。

        wxWidget並不能算是一個特別牛的庫,至少介面設計上還是MFC層級的,對xml也沒有能用的支援,拼個介面很累。但是你猜不到的是,就這麼爛的介面,用Python寫出來的程式依然不長。

        還有人用Python寫Win32的視窗程序,也不長。

        今天用到個libcurl,把C++版的測試代碼和Python版本貼出來對比一下。

import pycurlimport StringIO url = "http://www.google.com/"crl = pycurl.Curl()crl.setopt(pycurl.VERBOSE,1)crl.setopt(pycurl.FOLLOWLOCATION, 1)crl.setopt(pycurl.MAXREDIRS, 5)crl.fp = StringIO.StringIO()crl.setopt(pycurl.URL, url)crl.setopt(crl.WRITEFUNCTION, crl.fp.write)crl.perform()print crl.fp.getvalue()

#include "curl/curl.h"#pragma comment(lib, "libcurl.lib")long writer(void *data, int size, int nmemb, string &content);bool  CurlInit(CURL *&curl, const char* url,string &content);bool  GetURLDataBycurl(const char* URL, string &content);void main(){    char *url ="http://www.baidu.com/img/baidu.gif";    string content;    if ( GetURLDataBycurl(url,content))    {        printf("%s\n",content);    }    getchar();}bool CurlInit(CURL *&curl, const char* url,string &content){    CURLcode code;    string error;    curl = curl_easy_init();    if (curl == NULL)    {        printf( "Failed to create CURL connection\n");        return false;    }    code = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);    if (code != CURLE_OK)    {        printf( "Failed to set error buffer [%d]\n", code );        return false;    }    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);    code = curl_easy_setopt(curl, CURLOPT_URL, url);    if (code != CURLE_OK)    {        printf("Failed to set URL [%s]\n", error);        return false;    }    code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);    if (code != CURLE_OK)    {        printf( "Failed to set redirect option [%s]\n", error );        return false;    }    code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);    if (code != CURLE_OK)    {        printf( "Failed to set writer [%s]\n", error);        return false;    }    code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);    if (code != CURLE_OK)    {        printf( "Failed to set write data [%s]\n", error );        return false;    }    return true;}long writer(void *data, int size, int nmemb, string &content){    long sizes = size * nmemb;    string temp(data,sizes);    content += temp;     return sizes;}bool GetURLDataBycurl(const char* URL,  string &content){    CURL *curl = NULL;    CURLcode code;    string error;    code = curl_global_init(CURL_GLOBAL_DEFAULT);    if (code != CURLE_OK)    {        printf( "Failed to global init default [%d]\n", code );        return false;    }         if ( !CurlInit(curl,URL,content) )    {        printf( "Failed to global init default [%d]\n" );        return PM_FALSE;    }    code = curl_easy_perform(curl);    if (code != CURLE_OK)    {        printf( "Failed to get '%s' [%s]\n", URL, error);        return false;    }    long retcode = 0;    code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode);     if ( (code == CURLE_OK) && retcode == 200 )    {        double length = 0;        code = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD , &length);         printf("%d",retcode);        FILE * file = fopen("1.gif","wb");        fseek(file,0,SEEK_SET);        fwrite(content.c_str(),1,length,file);        fclose(file);        //struct curl_slist *list;        //code = curl_easy_getinfo(curl,CURLINFO_COOKIELIST,&list);        //curl_slist_free_all (list);        return true;    }    else    {    //    debug1( "%s \n ",getStatusCode(retcode));        return false;    }    curl_easy_cleanup(curl);    return false;}

        說Python開發快的都是廢話,這能不快嘛!!!!!!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.