码迷,mamicode.com
首页 > 编程语言 > 详细

Python pycurl

时间:2015-05-28 00:14:25      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

#!/usr/bin/env python
# -*- coding:utf-8 -*

from __future__ import print_function
import pycurl
import re
import sys
try:
    from io import BytesIO
except ImportError:
    from StringIO import StringIO as ByteIO

headers = {}
def header_function(header_line):
    header_line = header_line.decode(iso-8859-1)
    if ":" not in header_line:
        return

    name, value = header_line.split(":",1)
    name = name.strip()
    value = value.strip()
    name = name.lower()
    headers[name] = value

URL = http://pycurl.sourceforge.net
buffer =BytesIO()
c = pycurl.Curl()

c.setopt(c.URL,URL)
c.setopt(c.WRITEFUNCTION,buffer.write)
c.setopt(c.HEADERFUNCTION, header_function)
try:
    c.perform()
except Exception,e:
    print("connection error:" + str(e))
    buffer.close()
    c.close()
    sys.exit()

encoding = None
if content-type in headers:
    content_type = headers[content-type].lower()
    match = re.search(charset=(\S+), content_type)
    if match:
        encoding = match.group(1)
        print(Decoding using %s %encoding)

if encoding is None:
    encoding = iso-8859-1
    print(Assuming encoding is %s % encoding)

body = buffer.getvalue()
HTTP_CODE = c.getinfo(c.HTTP_CODE)
NAMELOOKUP_TIME = c.getinfo(c.NAMELOOKUP_TIME)
CONNECT_TIME = c.getinfo(c.CONNECT_TIME)
PRETRANSFER_TIME = c.getinfo(c.PRETRANSFER_TIME)
STARTTARNSFER_TIME = c.getinfo(c.STARTTRANSFER_TIME)
TOTAL_TIME = c.getinfo(c.TOTAL_TIME)
SIZE_DOWNLOAD = c.getinfo(c.SIZE_DOWNLOAD)
HEADER_SIZE = c.getinfo(c.HEADER_SIZE)
SPEED_DOWNLOAD = c.getinfo(c.SPEED_DOWNLOAD)

print("HTTP状态码:%s" % HTTP_CODE)
print("DNS解析时间:%.3f ms" %(NAMELOOKUP_TIME*1000))
print("建立连接时间:%.3f ms" %(CONNECT_TIME*1000))
print("准备传输时间:%.3f ms" %(PRETRANSFER_TIME*1000))
print("传输开始时间:%.3f ms" %(STARTTARNSFER_TIME*1000))
print("传输结束总时间:%.3f ms" %(TOTAL_TIME*1000))
print("下载数据包大小:%d bytes/s" % SIZE_DOWNLOAD)
print("HTTP头部大小:%d byte" %HEADER_SIZE)
print("平均下载速度:%d bytes/s" % SPEED_DOWNLOAD)

buffer.close()
c.close()

 

Python pycurl

标签:

原文地址:http://www.cnblogs.com/jachin/p/4534678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!