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

python中Urllib模块应用实例

时间:2015-07-28 14:51:49      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:urllib模块

# -*-coding:utf-8-*-
# urllib用于访问不需要验证的网络资源
# urllib.urlretrave(url,tempfile, functionLodingProcess,[form_data])提供了下载资源的功能。所需参数如其名,
# 不给定临时文件时,自动生成,返回文件名,和执行信息。
# 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 of  blocks 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)


本文出自 “magicpwn” 博客,请务必保留此出处http://magicpwn.blog.51cto.com/10497784/1679034

python中Urllib模块应用实例

标签:urllib模块

原文地址:http://magicpwn.blog.51cto.com/10497784/1679034

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