本文整理汇总了Python中twisted.python.failure.Failure.request方法的典型用法代码示例。如果您正苦于以下问题:Python Failure.request方法的具体用法?Python Failure.request怎么用?Python Failure.request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.python.failure.Failure
的用法示例。
在下文中一共展示了Failure.request方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_response
# 需要导入模块: from twisted.python.failure import Failure [as 别名]
# 或者: from twisted.python.failure.Failure import request [as 别名]
def process_response(self, response):
'''Passes response (Response or Failure object) received from
Downloader thought pipeline middlewares.
Return value is either Request, Response or Failure object.
'''
# we can be sure that response.request is set from the downloader
request = response.request
for name, enabled_setting, pr, pf in self._process_response:
# skip disabled mw through meta
if not request.meta.get(enabled_setting, True):
continue
method = pr if isinstance(response, Response) else pf
try:
response = method(response)
except:
response = Failure()
assert response is None or isinstance(response, (Request, Response, Failure)), \
'Middleware %s.process_request must return None, Response, Request or Failure, got %s' % \
(method.im_self.__class__.__name__, type(response))
if response is None:
method_name = 'process_response()' if method is pr else 'process_failure()'
failure = Failure(DropRequest(
'`%s` pipeline middleware dropped the request in `%s` method' %
(name, method_name)))
failure.request = request
return failure
if not isinstance(response, (Response, Failure)):
return response
# make sure, request attribute is always set
response.request = request
return response
示例2: _test_retry_exception
# 需要导入模块: from twisted.python.failure import Failure [as 别名]
# 或者: from twisted.python.failure.Failure import request [as 别名]
def _test_retry_exception(self, req, exception):
failure = Failure(exception)
# first retry
failure.request = req
req = self.mw.process_failure(failure)
self.assertIsInstance(req, Request)
self.assertEqual(req.meta['retry_times'], 1)
# second retry
failure.request = req
req = self.mw.process_failure(failure)
self.assertIsInstance(req, Request)
self.assertEqual(req.meta['retry_times'], 2)
# discard it
failure.request = req
req = self.mw.process_failure(failure)
self.assertIs(req, failure)
示例3: test_processing
# 需要导入模块: from twisted.python.failure import Failure [as 别名]
# 或者: from twisted.python.failure.Failure import request [as 别名]
def test_processing(self):
self.engine.start()
del self.sp.received[:]
# normal behavior
req = Request("http://github.com/")
resp = Response("", request=req)
self.engine.response_queue.push(resp)
self.clock.pump([self.engine.QUEUE_CHECK_FREQUENCY, 0])
self.check_signals([signals.response_downloaded, signals.response_received])
# download error
fail = Failure(Exception())
fail.request = req
self.engine.response_queue.push(fail)
self.clock.pump([self.engine.QUEUE_CHECK_FREQUENCY, 0, 0, 0])
self.check_signals([signals.failure_received, signals.spider_error])
# pipeline None
self.pipeline.resp = lambda req: None
self.engine.response_queue.push(resp)
self.clock.pump([self.engine.QUEUE_CHECK_FREQUENCY, 0, 0])
self.check_signals([signals.response_downloaded, signals.failure_received, signals.spider_error])
# pipeline request
self.pipeline.resp = lambda req: Request("http://github.com/")
self.engine.response_queue.push(resp)
self.clock.pump([self.engine.QUEUE_CHECK_FREQUENCY, 0, 0])
self.check_signals([signals.response_downloaded, signals.request_received])
self.assertEqual(len(self.engine.request_queue), 1)
self.engine.request_queue.pop()
# pipeline failure
self.pipeline.resp = lambda req: Failure(Exception())
self.engine.response_queue.push(resp)
self.clock.pump([self.engine.QUEUE_CHECK_FREQUENCY, 0, 0])
self.check_signals([signals.response_downloaded, signals.failure_received, signals.spider_error])