Examples of Curl Pycurl use in Python _python

Source: Internet
Author: User
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)

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.