On Linux There is a commonly used command curl (very useful), support curl is the famous Libcurl library, Libcurl is powerful, but also a very efficient library of functions. Libcurl in addition to providing its own C API, there are as many as 40 programming languages binding, the Pycurl described here is the Libcurl python binding.
In Python requests for get/post and so on, when the need to consider high-performance, Libcurl is a very good choice, generally more than Liburl, liburl2 faster, and may also be more efficient than requests. Especially when using Pycurl multiple concurrent requests, it is more efficient. Personal feeling, its only disadvantage is that, because it is directly called the Libcurl C library, Pycurl function interface and things like C, may not be so pythonic, write code learning curve slightly higher than the liburl.
Let's look at a simple example:
Copy Code code as follows:
#! /usr/bin/env python
#-*-Coding:utf-8-*-
'''
Created on Dec 15, 2013
@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. Effective_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)