本文整理汇总了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()
示例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()
示例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)
示例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
示例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