loinui 发表于 2015-7-29 10:34:27

python中Urllib模块应用实例

# -*-coding:utf-8-*-
# urllib用于访问不需要验证的网络资源
# urllib.urlretrave(url,tempfile, functionLodingProcess,)提供了下载资源的功能。所需参数如其名,
# 不给定临时文件时,自动生成,返回文件名,和执行信息。
# get 和post 是http的两种常用的方法,get-->就象明信片方式[内容显式放在URL中、内容长度受限、不安全]
# post-->信封模式[内容放在信封里,内容长度不受某些限制,安全]
# 实例get方式:
import urllib
import os


def reporthook(blocks_read, block_size, total_size):
    """total_size is reported in bytes;
    block_size is the amount read each time;
    block_read is the number ofblocks successful read.
    """
    if not blocks_read:
      print 'connection opened'
      return
    if total_size < 0:# 没有给定总大小就输出已获得多少数据
      print 'read %d blocks,(%d bytes)' % (blocks_read, blocks_read * block_size)
    else:# 给定了total_size值输出获得比值
      amount_read = block_size * blocks_read
      print 'read %d blocks, or %d/%d' % (blocks_read, amount_read, total_size)
    return


try:
    filename, msg = urllib.urlretrieve('http://cve.scap.org.cn/CVE-2015-4785.html', reporthook=reporthook)
finally:
    print 'File Exist?:', os.path.exists(filename), filename
    txt = file(filename)
    content = txt.read()
    print content
    urllib.urlcleanup()# 清理临时文件
    print 'File Exist?:', os.path.exists(filename)


页: [1]
查看完整版本: python中Urllib模块应用实例