本文整理汇总了Python中six.moves.http_client.IncompleteRead方法的典型用法代码示例。如果您正苦于以下问题:Python http_client.IncompleteRead方法的具体用法?Python http_client.IncompleteRead怎么用?Python http_client.IncompleteRead使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.http_client
的用法示例。
在下文中一共展示了http_client.IncompleteRead方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: iter_lines
# 需要导入模块: from six.moves import http_client [as 别名]
# 或者: from six.moves.http_client import IncompleteRead [as 别名]
def iter_lines(self):
kwargs = {
# OpenShift does not respond with any encoding value.
# This causes requests module to guess it as ISO-8859-1.
# Likely, the encoding is actually UTF-8, but we can't
# guarantee it. Therefore, we take the approach of simply
# passing through the encoded data with no effort to
# attempt decoding it.
'decode_unicode': False
}
if requests.__version__.startswith('2.6.'):
kwargs['chunk_size'] = 1
# if this fails for any reason other than ChunkedEncodingError
# or IncompleteRead (either of which may happen when no bytes
# are received), let someone else handle the exception
try:
for line in self.req.iter_lines(**kwargs):
yield line
except (requests.exceptions.ChunkedEncodingError,
http_client.IncompleteRead):
return
示例2: testDefaultExceptionHandler
# 需要导入模块: from six.moves import http_client [as 别名]
# 或者: from six.moves.http_client import IncompleteRead [as 别名]
def testDefaultExceptionHandler(self):
"""Ensures exception handles swallows (retries)"""
mock_http_content = 'content'.encode('utf8')
for exception_arg in (
http_client.BadStatusLine('line'),
http_client.IncompleteRead('partial'),
http_client.ResponseNotReady(),
socket.error(),
socket.gaierror(),
httplib2.ServerNotFoundError(),
ValueError(),
exceptions.RequestError(),
exceptions.BadStatusCodeError(
{'status': 503}, mock_http_content, 'url'),
exceptions.RetryAfterError(
{'status': 429}, mock_http_content, 'url', 0)):
retry_args = http_wrapper.ExceptionRetryArgs(
http={'connections': {}}, http_request=_MockHttpRequest(),
exc=exception_arg, num_retries=0, max_retry_wait=0,
total_wait_sec=0)
# Disable time.sleep for this handler as it is called with
# a minimum value of 1 second.
with patch('time.sleep', return_value=None):
http_wrapper.HandleExceptionsAndRebuildHttpConnections(
retry_args)
示例3: get_json
# 需要导入模块: from six.moves import http_client [as 别名]
# 或者: from six.moves.http_client import IncompleteRead [as 别名]
def get_json(self, url, **kwargs):
response = None
try:
response = self.session.get(url, **kwargs)
data = response.json()
if type(data) is dict and data.get('error'):
raise ServerResponsedError
return data
except ValueError as e:
retries = kwargs.get('retries', 0)
logger.error('when get <%d> %s, error %s (retry#%d)',
response.status_code, url, e, retries)
return {} if retries <= self.max_retries else \
self.get_json(url, retries=retries + 1)
except ServerResponsedError:
logger.error('when get <%d> %s, response: %s',
response.status_code, url, data)
return {}
except httplib.IncompleteRead as e:
logger.error('when get %s, error %s; partial: %s',
url, e, e.partial)
return {} # or shall we return `e.partial` ?
except RetryError as e:
logger.error('when get %s, retry error occurs. %s', url, e)
return {}
except Exception as e:
logger.error('error %s', e)
return {}
示例4: HandleExceptionsAndRebuildHttpConnections
# 需要导入模块: from six.moves import http_client [as 别名]
# 或者: from six.moves.http_client import IncompleteRead [as 别名]
def HandleExceptionsAndRebuildHttpConnections(retry_args):
"""Exception handler for http failures.
This catches known failures and rebuilds the underlying HTTP connections.
Args:
retry_args: An ExceptionRetryArgs tuple.
"""
# If the server indicates how long to wait, use that value. Otherwise,
# calculate the wait time on our own.
retry_after = None
# Transport failures
if isinstance(retry_args.exc, (http_client.BadStatusLine,
http_client.IncompleteRead,
http_client.ResponseNotReady)):
logging.debug('Caught HTTP error %s, retrying: %s',
type(retry_args.exc).__name__, retry_args.exc)
elif isinstance(retry_args.exc, socket.error):
logging.debug('Caught socket error, retrying: %s', retry_args.exc)
elif isinstance(retry_args.exc, socket.gaierror):
logging.debug(
'Caught socket address error, retrying: %s', retry_args.exc)
elif isinstance(retry_args.exc, socket.timeout):
logging.debug(
'Caught socket timeout error, retrying: %s', retry_args.exc)
elif isinstance(retry_args.exc, httplib2.ServerNotFoundError):
logging.debug(
'Caught server not found error, retrying: %s', retry_args.exc)
elif isinstance(retry_args.exc, ValueError):
# oauth2client tries to JSON-decode the response, which can result
# in a ValueError if the response was invalid. Until that is fixed in
# oauth2client, need to handle it here.
logging.debug('Response content was invalid (%s), retrying',
retry_args.exc)
elif (isinstance(retry_args.exc, TokenRefreshError) and
hasattr(retry_args.exc, 'status') and
(retry_args.exc.status == TOO_MANY_REQUESTS or
retry_args.exc.status >= 500)):
logging.debug(
'Caught transient credential refresh error (%s), retrying',
retry_args.exc)
elif isinstance(retry_args.exc, exceptions.RequestError):
logging.debug('Request returned no response, retrying')
# API-level failures
elif isinstance(retry_args.exc, exceptions.BadStatusCodeError):
logging.debug('Response returned status %s, retrying',
retry_args.exc.status_code)
elif isinstance(retry_args.exc, exceptions.RetryAfterError):
logging.debug('Response returned a retry-after header, retrying')
retry_after = retry_args.exc.retry_after
else:
raise retry_args.exc
RebuildHttpConnections(retry_args.http)
logging.debug('Retrying request to url %s after exception %s',
retry_args.http_request.url, retry_args.exc)
time.sleep(
retry_after or util.CalculateWaitForRetry(
retry_args.num_retries, max_wait=retry_args.max_retry_wait))
示例5: request
# 需要导入模块: from six.moves import http_client [as 别名]
# 或者: from six.moves.http_client import IncompleteRead [as 别名]
def request(*args, **kwargs):
url = kwargs.get('url') or \
(args[0] if isinstance(args[0], str) else args[2])
params = kwargs.get('params')
if kwargs.get('stream'):
return StreamResponse('./tests/data/' + 'sample.jpg')
for i, bundle in enumerate(MockedRequest.mapping):
regex, path = bundle
if regex.search(url) is not None:
json = JsonResponse('./tests/data/' + path)
if (i == 1
and kwargs.get('params')
and kwargs.get('params').get('before')):
global post_metas_requests
post_metas_requests += 1
if post_metas_requests >= 50: # cheating hacks Orz
return JsonResponse()
elif i == 4:
json.comments_case = True
json.start = params.get('after', 0) if params else 0
json.load_mockeddata()
return json
else:
if kwargs.get('error') == 'ValueError':
raise ValueError
elif kwargs.get('error') == 'IncompleteRead':
e = httplib.IncompleteRead(partial='some error here')
raise e
elif kwargs.get('error') == 'RetryError':
raise RetryError
elif kwargs.get('error') == 'Exception':
raise Exception
elif kwargs.get('resp_error'):
return JsonResponse(error=kwargs.get('resp_error'))
else:
error_json = JsonResponse()
error_json.result = {'error': 'Not found Ya'}
error_json.status_code = 404
return error_json
示例6: testHookErrors
# 需要导入模块: from six.moves import http_client [as 别名]
# 或者: from six.moves.http_client import IncompleteRead [as 别名]
def testHookErrors(self):
self.getPage('/demo/?id=1')
# If body is "razdrez", then on_end_request is being called too early.
self.assertBody('A horrorshow lomtick of cherry 3.14159')
# If this fails, then on_end_request isn't being called at all.
time.sleep(0.1)
self.getPage('/demo/ended/1')
self.assertBody('True')
valerr = '\n raise ValueError()\nValueError'
self.getPage('/demo/err?id=3')
# If body is "razdrez", then on_end_request is being called too early.
self.assertErrorPage(502, pattern=valerr)
# If this fails, then on_end_request isn't being called at all.
time.sleep(0.1)
self.getPage('/demo/ended/3')
self.assertBody('True')
# If body is "razdrez", then on_end_request is being called too early.
if (cherrypy.server.protocol_version == 'HTTP/1.0' or
getattr(cherrypy.server, 'using_apache', False)):
self.getPage('/demo/errinstream?id=5')
# Because this error is raised after the response body has
# started, the status should not change to an error status.
self.assertStatus('200 OK')
self.assertBody('nonconfidential')
else:
# Because this error is raised after the response body has
# started, and because it's chunked output, an error is raised by
# the HTTP client when it encounters incomplete output.
self.assertRaises((ValueError, IncompleteRead), self.getPage,
'/demo/errinstream?id=5')
# If this fails, then on_end_request isn't being called at all.
time.sleep(0.1)
self.getPage('/demo/ended/5')
self.assertBody('True')
# Test the "__call__" technique (compile-time decorator).
self.getPage('/demo/restricted')
self.assertErrorPage(401)
# Test compile-time decorator with kwargs from config.
self.getPage('/demo/userid')
self.assertBody('Welcome!')
示例7: testGzip
# 需要导入模块: from six.moves import http_client [as 别名]
# 或者: from six.moves.http_client import IncompleteRead [as 别名]
def testGzip(self):
zbuf = io.BytesIO()
zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
zfile.write(b'Hello, world')
zfile.close()
self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip')])
self.assertInBody(zbuf.getvalue()[:3])
self.assertHeader('Vary', 'Accept-Encoding')
self.assertHeader('Content-Encoding', 'gzip')
# Test when gzip is denied.
self.getPage('/gzip/', headers=[('Accept-Encoding', 'identity')])
self.assertHeader('Vary', 'Accept-Encoding')
self.assertNoHeader('Content-Encoding')
self.assertBody('Hello, world')
self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip;q=0')])
self.assertHeader('Vary', 'Accept-Encoding')
self.assertNoHeader('Content-Encoding')
self.assertBody('Hello, world')
# Test that trailing comma doesn't cause IndexError
# Ref: https://github.com/cherrypy/cherrypy/issues/988
self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip,deflate,')])
self.assertStatus(200)
self.assertNotInBody('IndexError')
self.getPage('/gzip/', headers=[('Accept-Encoding', '*;q=0')])
self.assertStatus(406)
self.assertNoHeader('Content-Encoding')
self.assertErrorPage(406, 'identity, gzip')
# Test for ticket #147
self.getPage('/gzip/noshow', headers=[('Accept-Encoding', 'gzip')])
self.assertNoHeader('Content-Encoding')
self.assertStatus(500)
self.assertErrorPage(500, pattern='IndexError\n')
# In this case, there's nothing we can do to deliver a
# readable page, since 1) the gzip header is already set,
# and 2) we may have already written some of the body.
# The fix is to never stream yields when using gzip.
if (cherrypy.server.protocol_version == 'HTTP/1.0' or
getattr(cherrypy.server, 'using_apache', False)):
self.getPage('/gzip/noshow_stream',
headers=[('Accept-Encoding', 'gzip')])
self.assertHeader('Content-Encoding', 'gzip')
self.assertInBody('\x1f\x8b\x08\x00')
else:
# The wsgiserver will simply stop sending data, and the HTTP client
# will error due to an incomplete chunk-encoded stream.
self.assertRaises((ValueError, IncompleteRead), self.getPage,
'/gzip/noshow_stream',
headers=[('Accept-Encoding', 'gzip')])