当前位置: 首页>>代码示例>>Python>>正文


Python urllib.ContentTooShortError方法代码示例

本文整理汇总了Python中urllib.ContentTooShortError方法的典型用法代码示例。如果您正苦于以下问题:Python urllib.ContentTooShortError方法的具体用法?Python urllib.ContentTooShortError怎么用?Python urllib.ContentTooShortError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在urllib的用法示例。


在下文中一共展示了urllib.ContentTooShortError方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_short_content_raises_ContentTooShortError

# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import ContentTooShortError [as 别名]
def test_short_content_raises_ContentTooShortError(self):
        self.fakehttp('''HTTP/1.1 200 OK
Date: Wed, 02 Jan 2008 03:03:54 GMT
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
Connection: close
Content-Length: 100
Content-Type: text/html; charset=iso-8859-1

FF
''')

        def _reporthook(par1, par2, par3):
            pass

        try:
            self.assertRaises(urllib.ContentTooShortError, urllib.urlretrieve,
                    'http://example.com', reporthook=_reporthook)
        finally:
            self.unfakehttp() 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:21,代码来源:test_urllib.py

示例2: test_short_content_raises_ContentTooShortError_without_reporthook

# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import ContentTooShortError [as 别名]
def test_short_content_raises_ContentTooShortError_without_reporthook(self):
        self.fakehttp('''HTTP/1.1 200 OK
Date: Wed, 02 Jan 2008 03:03:54 GMT
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
Connection: close
Content-Length: 100
Content-Type: text/html; charset=iso-8859-1

FF
''')
        try:
            self.assertRaises(urllib.ContentTooShortError, urllib.urlretrieve, 'http://example.com/')
        finally:
            self.unfakehttp() 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:16,代码来源:test_urllib.py

示例3: download_image

# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import ContentTooShortError [as 别名]
def download_image(self, img_src, img_path):
        try:
            urllib.request.urlretrieve(img_src, img_path)
        except urllib.ContentTooShortError as e:
            print(e)
            urllib.request.urlretrieve(img_src, img_path) 
开发者ID:SylvanasSun,项目名称:scrapy-picture-spider,代码行数:8,代码来源:pipelines.py

示例4: get_pic

# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import ContentTooShortError [as 别名]
def get_pic(self, playingsong, tempfile_path):
        '''获取专辑封面'''
        url = playingsong['picture'].replace('\\', '')
        for _ in range(3):
            try:
                urllib.urlretrieve(url, tempfile_path)
                logger.debug('Get cover art success!')
                return True
            except (IOError, urllib.ContentTooShortError):
                pass
        logger.error('Get cover art failed!')
        return False 
开发者ID:taizilongxu,项目名称:douban.fm,代码行数:14,代码来源:notification.py

示例5: urlretrieve

# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import ContentTooShortError [as 别名]
def urlretrieve(url: str, filename: str,
                    context: ssl.SSLContext, reporthook=None):
        """
        original source:
        https://github.com/python/cpython/blob/
        21bee0bd71e1ad270274499f9f58194ebb52e236/Lib/urllib/request.py#L229

        Because urlopen also supports context,
        I decided to adapt the download function.
        """
        url_parsed = urlparse.urlparse(url)

        with contextlib.closing(urllib.request.urlopen(url,
                                                       context=context)) as fp:
            headers = fp.info()

            # Just return the local path and the "headers" for file://
            # URLs. No sense in performing a copy unless requested.
            if url_parsed.scheme == "file" and not filename:
                return os.path.normpath(url_parsed.path), headers

            if not filename:
                raise RuntimeError("No filename specified!")

            tfp = open(filename, 'wb')

            with tfp:
                result = filename, headers

                # read overall
                read = 0

                # 4kb at once
                bs = 1024 * 8
                blocknum = 0

                # guess size
                size = int(headers.get("Content-Length", -1))

                if reporthook:
                    reporthook(blocknum, bs, size)

                while True:
                    block = fp.read(bs)
                    if not block:
                        break
                    read += len(block)
                    tfp.write(block)
                    blocknum += 1
                    if reporthook:
                        reporthook(blocknum, bs, size)

        if size >= 0 and read < size:
            raise urllib.ContentTooShortError(
                "retrieval incomplete: got only %i out of %i bytes"
                % (read, size), result)

        return result 
开发者ID:C0D3D3V,项目名称:Moodle-Downloader-2,代码行数:60,代码来源:url_target.py


注:本文中的urllib.ContentTooShortError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。