This article mainly introduces the example of using CURLPycURL in Python. if you need it, you can refer to the commonly used command curl in Linux (very useful). The popular libcurl library supports curl; 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:
The 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)