python类quote()的实例源码

test_urlparse.py 文件源码 项目:fast_urlparse 作者: Parsely 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_urlencode_quote_via(self):
        result = urlparse.urlencode({'a': 'some value'})
        self.assertEqual(result, "a=some+value")
        result = urlparse.urlencode({'a': 'some value/another'},
                                        quote_via=urlparse.quote)
        self.assertEqual(result, "a=some%20value%2Fanother")
        result = urlparse.urlencode({'a': 'some value/another'},
                                        safe='/', quote_via=urlparse.quote)
        self.assertEqual(result, "a=some%20value/another")
test_urlparse.py 文件源码 项目:fast_urlparse 作者: Parsely 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_quote_errors(self):
        self.assertRaises(TypeError, urlparse.quote, b'foo',
                          encoding='utf-8')
        self.assertRaises(TypeError, urlparse.quote, b'foo', errors='strict')
wget.py 文件源码 项目:altyazi 作者: my-old-projects 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def download(url, out=None, bar=bar_adaptive):
    """High level function, which downloads URL into tmp file in current
    directory and then renames it to filename autodetected from either URL
    or HTTP headers.

    :param bar: function to track download progress (visualize etc.)
    :param out: output filename or directory
    :return:    filename where URL is downloaded to
    """
    # detect of out is a directory
    outdir = None
    if out and os.path.isdir(out):
        outdir = out
        out = None

    # get filename for temp file in current directory
    prefix = detect_filename(url, out)
    (fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=prefix, dir=".")
    os.close(fd)
    os.unlink(tmpfile)

    # set progress monitoring callback
    def callback_charged(blocks, block_size, total_size):
        # 'closure' to set bar drawing function in callback
        callback_progress(blocks, block_size, total_size, bar_function=bar)
    if bar:
        callback = callback_charged
    else:
        callback = None

    if PY3K:
        # Python 3 can not quote URL as needed
        binurl = list(urlparse.urlsplit(url))
        binurl[2] = urlparse.quote(binurl[2])
        binurl = urlparse.urlunsplit(binurl)
    else:
        binurl = url
    (tmpfile, headers) = ulib.urlretrieve(binurl, tmpfile, callback)
    filename = detect_filename(url, out, headers)
    if outdir:
        filename = outdir + "/" + filename

    # add numeric ' (x)' suffix if filename already exists
    if os.path.exists(filename):
        filename = filename_fix_existing(filename)
    shutil.move(tmpfile, filename)

    # print headers
    return filename


问题


面经


文章

微信
公众号

扫码关注公众号