python pycurl模块高并发请求的例子

发布时间:2020-02-07编辑:脚本学堂
使用python pycurl模块实现高并发请求的例子,pycurl模块获取网页请求信息的方法示例,计算出页面的请求时间值。

python pycurl模块多并发请求时,效果非常高,直接调用libcurl c库。

代码:
 

复制代码 代码示例:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
 
'''
Created on Dec 15, 2013
 
@author: Jay
'''
 
import sys
import pycurl # 引入python pycurl模块
import time
 
class Test:
    def __init__(self):
        self.contents = ''
 
    def body_callback(self, buf):
        self.contents = self.contents + buf
 
sys.stderr.write("Testing %sn" % pycurl.version)
 
start_time = time.time()
 
url = 'http://www.jb200.com/php/'
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)