Example of CURL PycURL in Python

Source: Internet
Author: User

In Linux, there is a commonly used command curl (very useful), supporting curl is the well-known libcurl Library; libcurl is a powerful and efficient function library. In addition to providing its own c api, libcurl also has Binding in up to 40 programming languages. Here, PycURL is the Python binding of libcurl.
In Python, libcurl is a good choice for GET/POST requests on Web pages. libcurl is generally much faster than liburl and liburl2, it may be more efficient than Requests. Especially when using PycURL for multiple concurrent requests, it is highly efficient. Personally, The only drawback is that, because the libcurl C library is called directly, the function interfaces of PycURL are similar to things in C, and may not be so Pythonic, the learning curve for code writing is slightly higher than that for liburl.
Let's look at a simple example:
Copy codeThe Code is as follows:
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-

'''
Created on Dec 15,201 3

@ Author: Jay
'''

Import sys
Import pycurl
Import time

Class Test:
Def _ init _ (self ):
Self. contents =''

Def body_callback (self, buf ):
Self. contents = self. contents + buf

Sys. stderr. write ("Testing % s \ n" % pycurl. version)

Start_time = time. time ()

Url = 'HTTP: // www.dianping.com/shanghai'
T = Test ()
C = pycurl. Curl ()
C. setopt (c. URL, url)
C. setopt (c. WRITEFUNCTION, t. body_callback)
C. perform ()
End_time = time. time ()
Duration = end_time-start_time
Print c. getinfo (pycurl. HTTP_CODE), c. getinfo (pycurl. inclutive_url)
C. close ()

Print 'pycurl takes % s seconds to get % s' % (duration, url)

Print 'lenth of the content is % d' % len (t. contents)
# Print (t. contents)

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.