2012年5月28日星期一

pycurl简介

    一,简介与特性

    PycURLpython的库,通过python封装了libcurl的调用,pycurl本身的文档不多,主要的说明可以参考libcurl的文档。

    pycurl是一个成熟、非常快速并且支持多种特性。

    Overview

    • libcurl is a free and easy-to-use client-side URL transfer library, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS, FILE, IMAP, SMTP, POP3 and RTSP. libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos4), file transfer resume, http proxy tunneling and more!
    • libcurl is highly portable, it builds and works identically on numerous platforms, including Solaris, NetBSD, FreeBSD, OpenBSD, Darwin, HPUX, IRIX, AIX, Tru64, Linux, UnixWare, HURD, Windows, Amiga, OS/2, BeOs, Mac OS X, Ultrix, QNX, OpenVMS, RISC OS, Novell NetWare, DOS and more...
    • libcurl is free, thread-safe, IPv6 compatible, feature rich, well supported, fast, thoroughly documented and is already used by many known, big and successful companies and numerous applications.

     

    源文档 <http://pycurl.sourceforge.net/>

     

     

    二,与其他库对比

     

    Urllib2

    Urlgrabber

    Pycurl

    简介

    python程序,设计完善、灵活。

    比较基础

    python程序,基于urllib2yum实现的一部分

    libcurl的封装,libcurl本身是c库,非常快,支持多种协议

    优势

    1python自带,不依赖于外来库

    2)纯python,简单易用,可自己在其基础上定制

    1)比urllib2更友好的用户界面,封装了很多常用的基本功能

    2)易于修改。

    1)快

    2)支持多种协议,类似SSL-ed FTP

    劣势

    1)比pycurl

    2)对低层协议的封装比较简陋,如果需要更多的功能,需要有较多的开发时间

    1)比pycurl慢,某种程度上比urllib2略慢。不过封装的程序基本如此。

    1),使用方式比较复杂。有比较陡的学习曲线。

    调用方式C-style

    适用范围

    只需要基础的功能,比如url访问等,或者需要再其基础上作定制封装。

    需要较多的功能,灵活。

    很快的速度要求,需要自己做调整,必要的协议支持。

    http://urlgrabber.baseurl.org/comparison.html

    三,使用方式

    pycurl的文档的说明主要参考libcurl的文档说明,一些基本的用法,可以参考pycurltestexample目录

    http://pycurl.cvs.sourceforge.net/pycurl/pycurl/tests/

    http://pycurl.cvs.sourceforge.net/pycurl/pycurl/examples/

     

    基本的使用方式:

    1

    #! /usr/bin/env python

    2

    # -*- coding: iso-8859-1 -*-

    3

    # vi:ts=4:et

    4

    # $Id: test_cb.py,v 1.14 2003/04/21 18:46:10 mfx Exp $

    5

     

    6

    import sys

    7

    import pycurl

    8

     

    9

    ## Callback function invoked when body data is ready

    10

    def body(buf):

    11

    # Print body data to stdout

    12

    sys.stdout.write(buf)

    13

     

    14

    ## Callback function invoked when header data is ready

    15

    def header(buf):

    16

    # Print header data to stderr

    17

    sys.stderr.write(buf)

    18

     

    19

    c = pycurl.Curl()

    20

    c.setopt(pycurl.URL, 'http://www.python.org/')

    21

    c.setopt(pycurl.WRITEFUNCTION, body)

    22

    c.setopt(pycurl.HEADERFUNCTION, header)

    23

    c.setopt(pycurl.FOLLOWLOCATION, 1)

    24

    c.setopt(pycurl.MAXREDIRS, 5)

    25

    c.perform()

    26

    c.setopt(pycurl.URL, 'http://curl.haxx.se/')

    27

    c.perform()

    28

    c.close()

     

    源文档 <http://pycurl.cvs.sourceforge.net/viewvc/pycurl/pycurl/tests/test_cb.py?view=markup>

     

    重要的是setopt方法,setopt方法对应的参数的说明见

    http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

    基本的对应为

    Pycurl

    Libcurl

     

    FOLLOWLOCATION

    CURLOPT_FOLLOWLOCATION

     

    诸如此类……

     

     

     

    参考:

    http://pycurl.sourceforge.net/

    http://curl.haxx.se/libcurl/

     

没有评论: