这篇文章主要介绍了Python中的CURL PycURL使用例子,需要的朋友可以参考下
在Python中对网页进行GET/POST等请求,当需要考虑高性能的时候,libcurl是非常不错的选择,一般来说会比liburl、liburl2快不少,可能也会比Requests的效率更高。特别是使用PycURL的多并发请求时,更是效率很高的。个人感觉,其唯一的缺点是,由于是直接调用的是libcurl C库,PycURL的函数接口之类的还和C中的东西很像,可能不是那么的Pythonic,写代码的学习曲线稍微比liburl高一点儿。
还是看个简单的例子吧:
# -*- c来源gao@!dai!ma.com搞$$代^@码!网oding: 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)
以上就是Python中的CURL PycURL使用例子的详细内容,更多请关注gaodaima搞代码网其它相关文章!