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)