本文整理匯總了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