Pycurl,python CURL Library

Source: Internet
Author: User
Tags ftp file http post ldap gopher

Pycurl-a Python interface to the CURL library

The Pycurl package is a Libcurl python interface. Pycurl has been successful in the Python2.2 to Python2.5 version of the compilation Test.

Libcurl is a client URL transfer library that supports FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP. Libcurl also supports HTTPS authentication, HTTP post,http put,ftp on Transmission, Proxy, Cookies, Basic authentication, FTP file breakpoint relay, HTTP proxy channel and so on.

All the features provided by Libcurl can be used through the Pycurl interface.

Module functionality

Pycurl.global_init (option)->none
The option is one of the following constants: Pycurl. Global_ssl, Pycurl. Global_win32, Pycurl. Global_all, Pycurl. Global_nothing, Pycurl. Global_default. The corresponding is the Curl_global_init () method of the Libcurl.

Pycurl.global_cleanup ()-None
The corresponding is the Curl_global_cleanup () method of the Libcurl.

Pycurl.version
This is the information for the current version of Liburl, which corresponds to the Liburl curl_version () method.
Examples of usage:

>>> Import Pycurl
>>> pycurl.version
' libcurl/7.12.3 openssl/0.9.7e zlib/1.2.2.1 libidn/0.5.12 '

Pycurl.version_info (), Tuple
Corresponds to the Curl_version_info () method in the Libcurl. Returns a sequence of information like the Curl_version_info_data structured data returned by the Curl_version_info () method of Liburl.
Examples of usage:

>>> Import Pycurl
>>> Pycurl.version_info ()
(2, ' 7.12.3 ', 461827, ' I586-pc-linux-gnu ', 1565, ' openssl/0.9.7e ', 9465951,
' 1.2.2.1 ', (' ftp ', ' gopher ', ' telnet ', ' dict ', ' LDAP ', ' http ', ' file ',
' https ', ' FTPs '), None, 0, ' 0.5.12 ')

Pycurl. Curl (), Curl object
This function creates a curl object corresponding to the Curl processor in the Libcurl. The Curl object automatically sets Curlopt_verbose to 0 and curlopt_noprogress to 1, providing a default curlopt_useragent and setting Curlopt_errorbuffer pointing to a private error buffer.

Pycurl. Curlmulti (), Curlmulti object
This function creates a new Curlmulti object that corresponds to the CURLM processor in Libcurl.

Pycurl. Curlshare (), Curlshare object
This function creates a new Curlshare object that corresponds to the Curlsh processor in Libcurl. The Curlshare object can pass the share option parameter on the Curl object.

Subsections
  • Curl objects
  • Curlmulti objects
  • Curlshare objects
  • Callbackscurl Object

    The Curl object has the following methods:

    Close (), None
    Corresponds to the Curl_easy_cleanup method in Libcurl. Pycurl calls this method automatically when the Curl object is no longer referenced, but it can also be called directly.

    Perform ()-None
    Corresponds to the Curl_easy_perform method in Libcurl.

    Setopt (option, value), None
    corresponding to the Curl_easy_setopt method in Libcurl, option is specified using the curlopt_* constant in Libcurl, but the curlopt_ prefix is now removed. The data type of value is dependent on option, It can be a string, an integer, a long integer, a file object, a list, or a function.
    Examples of usage:

    Import Pycurl
    c = Pycurl. Curl ()
    C.setopt (Pycurl. URL, "http://www.python.org/")
    C.setopt (Pycurl. Httpheader, ["Accept:"])
    Import Stringio
    b = Stringio.stringio ()
    C.setopt (Pycurl. Writefunction, B.write)
    C.setopt (Pycurl. Followlocation, 1)
    C.setopt (Pycurl. Maxredirs, 5)
    C.perform ()
    Print B.getvalue ()

    GetInfo (option), Result
    For the Curl_easy_getinfo method in Libcurl, option is also specified using the Curlopt_* constant in Libcurl, but the curlopt_ prefix is now removed. Result contains an integer, floating-point number, or string, which is believed to be a given Option.getinfo method that cannot be called before the Perform method is called or completed.
    Examples of usage:

    Errstr (), String
    Returns a string representation of the internal Libcurl error buffer in this processor.

    Curlmulti Object


    The Curlmulti object has the following methods:

    Close (), None
    Corresponds to the Curl_multi_cleanup () method in Libcurl. Pycurl calls the method automatically when the Curlmulti object is no longer referenced, or it can be displayed.

    Perform (), tuple of status and the number of active Curl objects
    Corresponds to the Curl_multi_perform () method in Libcurl.

    Add_handle (Curl object), None
    Corresponds to the Curl_multi_add_handle () method in Libcurl. This method adds a valid Curl object to the Curlmulti object.
    Important: Add_handle does not implicitly add a reference to the Curl object (and thus does not increase the number of references to the Curl object)

    Remove_handle (Curl object), None
    Corresponds to the Curl_multi_remove_handle () method in Libcurl. This method removes an existing Curl object from the Curlmulti object.
    Important: Remove_handle does not implicitly remove references to curl objects (thus reducing the number of references to curl objects).

    Fdset () Triple of lists with active file descriptors, readable, writeable, exceptions.
    Corresponds to the Curl_multi_fdset () method in Libcurl. This method extracts the file description information from the Curlmulti object. The returned list can be used for select modules to poll for events.
    Examples of usage:

    Import Pycurl
    c = Pycurl. Curl ()
    C.setopt (Pycurl. URL, "http://curl.haxx.se")
    m = Pycurl. Curlmulti ()
    M.add_handle (c)
    While 1:
    RET, num_handles = M.perform ()
    IF ret! = Pycurl. E_call_multi_perform:break
    While Num_handles:
    Apply (Select.select, M.fdset () + (1,))
    While 1:
    RET, num_handles = M.perform ()
    IF ret! = Pycurl. E_call_multi_perform:break

    Select (timeout), number of ready file descriptors or-1 on timeout
    This is a useful function that simplifies the use of combination of fdest () and select modules.
    Examples of usage:

    Import Pycurl
    c = Pycurl. Curl ()
    C.setopt (Pycurl. URL, "http://curl.haxx.se")
    m = Pycurl. Curlmulti ()
    M.add_handle (c)
    While 1:
    RET, num_handles = M.perform ()
    IF ret! = Pycurl. E_call_multi_perform:break
    While Num_handles:
    ret = M.select (1.0)
    if ret = = -1:continue
    While 1:
    RET, num_handles = M.perform ()
    IF ret! = Pycurl. E_call_multi_perform:break

    Info_read ([Max]), Numberof queued messages, a list of successful objects, a list of failed objects
    Corresponds to the Curl_multi_info_read () method in Libcurl. This method extracts up to max information from multiple stacks and then returns two lists. The first list contains the operations that completed successfully the second list contains the <curl objects for each failed Curl object. Curl error code, curl error message > sequence.

    Curlshare Object

    The Curlshare object has the following methods:

    Setopt (option, value), None
    corresponding to the Curl_share_setopt method in Libcurl, option is specified using the curlopt_* constant in Libcurl, but the curlopt_ prefix is now changed to Sh_. Usually value must be Lock_data_ Cookies, or Lock_data_dns.
    Examples of usage:

    Import Pycurl
    Curl = Pycurl. Curl ()
    s = Pycurl. Curlshare ()
    S.setopt (Pycurl. Sh_share, Pycurl. Lock_data_cookie)
    S.setopt (Pycurl. Sh_share, Pycurl. LOCK_DATA_DNS)
    Curl.setopt (Pycurl. URL, ' http://curl.haxx.se ')
    Curl.setopt (Pycurl. SHARE, s)
    Curl.perform ()
    Curl.close ()
    Callbacks

    For better control, Libcurl allows some callback functions to be associated with each connection. In Pycurl, the callback function calls setopt through the Curl object for S writefunction, Readfunction, Headerfunction, Progressfunction, ioctlfunction, or debugfunction these options are set. These options correspond to Libcurl curlopt_* The option to remove the prefix. In Pycurl, the callback function must be a normal Python function, or a method of a class or an extended function type.

    Some of the limitations here are that the callback functions for these options can occur at the same time. It allows different callback functions to correspond to different curl objects. More specifically, WriteData's callback function cannot be used for writefunction, The callback function of ReadData cannot be used for Readfunction,writeheader callback function cannot be used for Headerfunction,progressdata callback function cannot be used for progressfunction, The Ioctldata callback function cannot be used for Ioctlfunction,debugdata callback functions that cannot be used in debugfunction. You can overcome this limitation by using an instance method of a class as a callback function and storing the data for each object like a file object with class instance properties.

    The signature of each callback function in Pycurl is as follows:


    Writefunction (String), number of characters written

    Readfunction (number of characters to read), string

    Headerfunction (String), number of characters written

    Progressfunction (download total, downloaded, upload total, uploaded), status

    Debugfunction (Debug message type, debug message string), None

    Ioctlfunction (ioctl cmd), status

    Example:callbacks for document header and body

    This example prints the head data to the STDERR print content data to stdout. Also note that neither of them returns the number of bytes written. Writefunction and Headerfunction callback, which returns none when all bytes are written.

    # # Callback function invoked when body data was ready
    def body (BUF):
    # Print body data to stdout
    Import Sys
    Sys.stdout.write (BUF)
    # returning None implies that all bytes were written

    # # Callback function invoked when header data are ready
    def header (BUF):
    # Print Header data to stderr
    Import Sys
    Sys.stderr.write (BUF)
    # returning None implies that all bytes were written

    c = Pycurl. Curl ()
    C.setopt (Pycurl. URL, "http://www.python.org/")
    C.setopt (Pycurl. Writefunction, body)
    C.setopt (Pycurl. Headerfunction, header)
    C.perform () Example:download/upload Progress callback

    This example shows how to use a progress callback. When you download a document, the uploads parameter will be 0 and vice versa.

    # # Callback function invoked when download/upload have progress
    def progress (download_t, Download_d, upload_t, upload_d):
    Print "Total to download", download_t
    Print "Total downloaded", Download_d
    Print "Total to upload", upload_t
    Print "Total uploaded", upload_d

    C.setopt (C.url, "http://slashdot.org/")
    C.setopt (c.noprogress, 0)
    C.setopt (C.progressfunction, progress)
    C.perform () Example:debug callbacks

    This example shows how to use Debug callbacks. The Debug information type is an integer indicating type of debug information. The verbose option must be available when this callback is invoked.

    def test (Debug_type, debug_msg):
    Print "Debug (%d):%s"% (Debug_type, debug_msg)

    c = Pycurl. Curl ()
    C.setopt (Pycurl. URL, "http://curl.haxx.se/")
    C.setopt (Pycurl. VERBOSE, 1)
    C.setopt (Pycurl. Debugfunction, test)
    C.perform ()
    Other examples

    Pycrul also contains test scripts and cases that demonstrate how to use different callbacks in Libcurl. For example, the file examples/file_upload.py contains the case code for how to use Readfunction, ' Tests/test_ cb.py ' demo writefunction and Headerfunction, ' tests/test_debug.py ' demo debugfunction, ' tests/test_getinfo.py ' Demo Progressfunction.

    ============================================================================

    Edit text saved under C:/python25/lib pycurl_test.py

    Import Pycurl
    Import Stringi

    def geturlcontent_pycurl (URL):
    c = Pycurl. Curl ()
    C.setopt (Pycurl. Url,url)
    b = Stringio.stringio ()
    C.setopt (Pycurl. Writefunction, B.write)
    C.setopt (Pycurl. Followlocation, 1)
    C.setopt (Pycurl. Maxredirs, 5)
    #c. setopt (Pycurl. PROXY, ' http://11.11.11.11:8080 ')
    #c. setopt (Pycurl. Proxyuserpwd, ' aaa:aaa ')
    C.perform ()
    Return B.getvalue ()

    Enter import pycurl_test on the command line

    url = ' Http://blog.csdn.net '

    Content = Geturlcontent_pycurl (URL)

    Print content

    =============================================================

Pycurl,python CURL Library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.