感嘆!神馬のものはpythonで書いても大きく短くなります!


前回はwxWidgetでしたが、今回はまたcURLを発見.pythonバージョンはwxPythonとpycurlと呼ばれています.
wxWidgetは特別な牛のライブラリとは言えませんが、少なくともインタフェースの設計上はMFCレベルで、xmlに対しても使えるサポートがなく、インタフェースをつづるのは疲れています.しかし、あなたが推測できないのは、このような腐ったインタフェースで、Pythonで書かれたプログラムは依然として長くありません.
PythonでWin 32のウィンドウプログラムを書く人もいますが、長くはありません.
今日はlibcurlを使って、C++版のテストコードとPython版を貼り合わせて比較します.
import pycurl
import 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
",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
"); return false; } code = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error); if (code != CURLE_OK) { printf( "Failed to set error buffer [%d]
", 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]
", error); return false; } code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); if (code != CURLE_OK) { printf( "Failed to set redirect option [%s]
", error ); return false; } code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); if (code != CURLE_OK) { printf( "Failed to set writer [%s]
", error); return false; } code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content); if (code != CURLE_OK) { printf( "Failed to set write data [%s]
", 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]
", code ); return false; } if ( !CurlInit(curl,URL,content) ) { printf( "Failed to global init default [%d]
" ); return PM_FALSE; } code = curl_easy_perform(curl); if (code != CURLE_OK) { printf( "Failed to get '%s' [%s]
", 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
",getStatusCode(retcode)); return false; } curl_easy_cleanup(curl); return false; }

Pythonの开発が速いと言うのはくだらない话で、これは速くないことができます!!!