本文整理汇总了Python中requests.exceptions.ChunkedEncodingError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.ChunkedEncodingError方法的具体用法?Python exceptions.ChunkedEncodingError怎么用?Python exceptions.ChunkedEncodingError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.exceptions
的用法示例。
在下文中一共展示了exceptions.ChunkedEncodingError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_stream
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def get_stream(url, headers=None, encoding='UTF-8'):
"""分块接受数据"""
try:
lines = requests.get(url, headers=headers, timeout=_tiemout, stream=True, proxies=proxies)
html = list()
for line in lines.iter_lines():
if b'\x00' in line:
break
line = line.decode(encoding)
html.append(line.strip())
return '\r\n'.join(html).strip()
except ChunkedEncodingError as e:
return '\r\n'.join(html).strip()
except ConnectionError as e:
return "ERROR:" + "HTTP连接错误"
except ConnectTimeout as e:
return "ERROR:" + "HTTP连接超时错误"
except Exception as e:
return 'ERROR:' + str(e)
示例2: post_stream
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def post_stream(url, data=None, headers=None, encoding='UTF-8', files=None):
"""分块接受数据"""
try:
lines = requests.post(url, data=data, headers=headers, timeout=_tiemout, stream=True, proxies=proxies,
files=None)
html = list()
for line in lines.iter_lines():
line = line.decode(encoding)
html.append(line.strip())
return '\r\n'.join(html).strip()
except ChunkedEncodingError as e:
return '\r\n'.join(html).strip()
except ConnectionError as e:
return "ERROR:" + "HTTP连接错误"
except ConnectTimeout as e:
return "ERROR:" + "HTTP连接超时错误"
except Exception as e:
return 'ERROR:' + str(e)
示例3: test_download_corrupted_apk
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def test_download_corrupted_apk(self, playstore, download_folder_path, monkeypatch):
# noinspection PyUnusedLocal
def raise_exception(*args, **kwargs):
raise ChunkedEncodingError()
monkeypatch.setattr(Util, "show_list_progress", raise_exception)
# Mock the function that gets the size of the file so that the downloaded
# apk will be treated as corrupted.
monkeypatch.setattr(os.path, "getsize", lambda x: 1)
# Simulate an error with the file deletion.
# noinspection PyUnusedLocal
def raise_os_error(ignore):
raise OSError
monkeypatch.setattr(os, "remove", raise_os_error)
result = playstore.download(
VALID_PACKAGE_NAME,
os.path.join(download_folder_path, f"{VALID_PACKAGE_NAME}.apk"),
)
assert result is False
示例4: test_download_corrupted_split_apk
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def test_download_corrupted_split_apk(
self, playstore, download_folder_path, monkeypatch
):
original = Util.show_list_progress
def raise_exception(*args, **kwargs):
if " split apk ".lower() not in kwargs["description"].lower():
return original(*args, **kwargs)
else:
raise ChunkedEncodingError()
monkeypatch.setattr(Util, "show_list_progress", raise_exception)
result = playstore.download(
APK_WITH_SPLIT_APK,
os.path.join(download_folder_path, f"{APK_WITH_SPLIT_APK}.apk"),
download_split_apks=True,
show_progress_bar=False,
)
assert result is False
示例5: test_download_corrupted_obb
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def test_download_corrupted_obb(self, playstore, download_folder_path, monkeypatch):
original = Util.show_list_progress
def raise_exception(*args, **kwargs):
if " .obb ".lower() not in kwargs["description"].lower():
return original(*args, **kwargs)
else:
raise ChunkedEncodingError()
monkeypatch.setattr(Util, "show_list_progress", raise_exception)
result = playstore.download(
APK_WITH_OBB,
os.path.join(download_folder_path, f"{APK_WITH_OBB}.apk"),
download_obb=True,
show_progress_bar=False,
)
assert result is False
示例6: listen_for_dweets_from
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def listen_for_dweets_from(thing_name, timeout=900, key=None, session=None):
"""Create a real-time subscription to dweets
"""
url = BASE_URL + '/listen/for/dweets/from/{0}'.format(thing_name)
session = session or requests.Session()
if key is not None:
params = {'key': key}
else:
params = None
start = datetime.datetime.utcnow()
while True:
request = requests.Request("GET", url, params=params).prepare()
resp = session.send(request, stream=True, timeout=timeout)
try:
for x in _listen_for_dweets_from_response(resp):
yield x
_check_stream_timeout(start, timeout)
except (ChunkedEncodingError, requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout):
pass
_check_stream_timeout(start, timeout)
示例7: test_watch_client_request_failed
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def test_watch_client_request_failed(self, m_sys_exit):
path = '/test'
m_handler = mock.Mock()
watcher_obj = self._test_watch_create_watcher(path, m_handler)
watcher_obj._watch(path)
self.client.watch.side_effect = exceptions.ChunkedEncodingError(
"Connection Broken")
self.client.watch.assert_called_once()
self.assertFalse(watcher_obj._alive)
m_sys_exit.assert_called_once_with(1)
示例8: test_watch_retry
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def test_watch_retry(self, m_sys_exit):
path = '/test'
events = [{'e': i} for i in range(3)]
side_effects = [exceptions.ChunkedEncodingError("Connection Broken")]
side_effects.extend(None for _ in events)
m_handler = mock.Mock()
m_handler.side_effect = side_effects
watcher_obj = self._test_watch_create_watcher(path, m_handler, 10)
self._test_watch_mock_events(watcher_obj, events)
watcher_obj._watch(path)
m_handler.assert_has_calls([mock.call(e) for e in events])
m_sys_exit.assert_called_once_with(1)
示例9: get
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def get(url, headers=None, encoding='UTF-8'):
"""GET请求发送包装"""
try:
html = requests.get(url, headers=headers, proxies=proxies, timeout=_tiemout)
html = html.content.decode(encoding)
return html.replace('\x00', '').strip()
except ChunkedEncodingError as e:
html = get_stream(url, headers, encoding)
return html
except ConnectionError as e:
return "ERROR:" + "HTTP连接错误"
except ConnectTimeout as e:
return "ERROR:" + "HTTP连接超时错误"
except Exception as e:
return 'ERROR:' + str(e)
示例10: post
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def post(url, data=None, headers=None, encoding='UTF-8', files=None):
"""POST请求发送包装"""
try:
html = requests.post(url, data=data, headers=headers, proxies=proxies, timeout=_tiemout, files=files)
html = html.content.decode(encoding)
return html.replace('\x00', '').strip()
except ChunkedEncodingError as e:
html = post_stream(url, data, headers, encoding, files)
return html
except ConnectionError as e:
return "ERROR:" + "HTTP连接错误"
except ConnectTimeout as e:
return "ERROR:" + "HTTP连接超时错误"
except Exception as e:
return 'ERROR:' + str(e)
示例11: write
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def write(self, sequence, res, chunk_size=8192):
if sequence.segment.key and sequence.segment.key.method != "NONE":
try:
decryptor = self.create_decryptor(sequence.segment.key,
sequence.num)
except StreamError as err:
log.error("Failed to create decryptor: {0}", err)
self.close()
return
data = res.content
# If the input data is not a multiple of 16, cut off any garbage
garbage_len = len(data) % 16
if garbage_len:
log.debug("Cutting off {0} bytes of garbage "
"before decrypting", garbage_len)
decrypted_chunk = decryptor.decrypt(data[:-garbage_len])
else:
decrypted_chunk = decryptor.decrypt(data)
self.reader.buffer.write(pkcs7_decode(decrypted_chunk))
else:
try:
for chunk in res.iter_content(chunk_size):
self.reader.buffer.write(chunk)
except ChunkedEncodingError:
log.error("Download of segment {0} failed", sequence.num)
return
log.debug("Download of segment {0} complete", sequence.num)
示例12: connection_handler
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def connection_handler(session, request, verify, as_is_reply=False):
air = as_is_reply
s = session
r = request
v = verify
return_json = False
disable_warnings(InsecureRequestWarning)
try:
get = s.send(r, verify=v)
if get.status_code == 401:
if 'NoSiteContext' in str(get.content):
logger.info('Your Site is incorrect for %s', r.url)
elif 'LoginRequired' in str(get.content):
logger.info('Your login credentials are incorrect for %s', r.url)
else:
logger.info('Your api key is incorrect for %s', r.url)
elif get.status_code == 404:
logger.info('This url doesnt even resolve: %s', r.url)
elif get.status_code == 200:
try:
return_json = get.json()
except JSONDecodeError:
logger.error('No JSON response. Response is: %s', get.text)
if air:
return get
except InvalidSchema:
logger.error("You added http(s):// in the config file. Don't do that.")
except SSLError as e:
logger.error('Either your host is unreachable or you have an SSL issue. : %s', e)
except ConnectionError as e:
logger.error('Cannot resolve the url/ip/port. Check connectivity. Error: %s', e)
except ChunkedEncodingError as e:
logger.error('Broken connection during request... oops? Error: %s', e)
return return_json
示例13: main
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def main():
count = 0
while True:
# print('第 ',count,' 次测试')
count = count + 1
try:
#请求不同的代理和headers
global headers,count_proxys
headers = {'User-Agent': ua.random}
count_proxys = get_count_proxys()
print('代理总数: ',count_proxys,' 当前所用的代理:',proxy,'\n',headers)
start_time = time.clock()
html = crawl('http://www.baidu.com', proxy)
end_time = time.clock()
print('代理连接时间: ',(str(end_time-start_time))[:4],' 秒')
if html.status_code==200:
print(html)
return count
break
elif count>=10:
print('抓取网页失败')
break
except (ChunkedEncodingError,ConnectionError,Timeout,UnboundLocalError,UnicodeError,ProxyError):
global proxy
proxy = get_proxy()
print('代理失败,更换代理','\n')
# print(' ')
示例14: _get_member_by_mid
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def _get_member_by_mid(self, mid: int) -> Optional[dict]:
"""
根据用户id获取其信息
:param mid: B站用户id
:return: 用户详情 or None
"""
get_params = {
'mid': mid,
'jsonp': 'jsonp'
}
try:
res_json = requests.get(API_MEMBER_INFO, params=get_params, timeout=WAIT_MAX, proxies=self.cur_proxy,
headers=self.headers).json()
except ConnectTimeout as e:
print(f'获取用户id: {mid} 详情失败: 请求接口超时, 当前代理:{self.cur_proxy["https"]}')
raise RequestException(str(e))
except ReadTimeout as e:
print(f'获取用户id: {mid} 详情失败: 接口读取超时, 当前代理:{self.cur_proxy["https"]}')
raise RequestException(str(e))
except ValueError as e:
# 解析json失败基本上就是ip被封了
print(f'获取用户id: {mid} 详情失败: 解析json出错, 当前代理:{self.cur_proxy["https"]}')
raise RequestException(str(e))
except ProxyError as e:
print(f'获取用户id: {mid} 详情失败: 连接代理失败, 当前代理:{self.cur_proxy["https"]}')
raise RequestException(str(e))
except requests.ConnectionError as e:
# 可以断定就是代理IP地址无效
print(f'获取用户id: {mid} 详情失败: 连接错误, 当前代理:{self.cur_proxy["https"]}')
raise RequestException(str(e))
except ChunkedEncodingError as e:
print(f'获取用户id: {mid} 详情失败: 远程主机强迫关闭了一个现有的连接, 当前代理:{self.cur_proxy["https"]}')
raise RequestException(str(e))
else:
if res_json['code'] == -404:
print(f'找不到用户mid:{mid}')
raise UserNotFoundException(f'找不到用户mid:{mid}')
if 'data' in res_json:
return res_json['data']
print(f'获取用户id: {mid} 详情失败: data字段不存在!')
return
示例15: __retry
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ChunkedEncodingError [as 别名]
def __retry(self, request, retries=0):
try:
return request()
except ChunkedEncodingError as e:
if retries != self.max_retries:
return self.__retry(request, retries=retries + 1)
else:
raise e