本文整理汇总了Python中twisted.web.iweb.IBodyProducer方法的典型用法代码示例。如果您正苦于以下问题:Python iweb.IBodyProducer方法的具体用法?Python iweb.IBodyProducer怎么用?Python iweb.IBodyProducer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.iweb
的用法示例。
在下文中一共展示了iweb.IBodyProducer方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: startProducing
# 需要导入模块: from twisted.web import iweb [as 别名]
# 或者: from twisted.web.iweb import IBodyProducer [as 别名]
def startProducing(self, consumer):
"""
Start a cooperative task which will read bytes from the input file and
write them to C{consumer}. Return a L{Deferred} which fires after all
bytes have been written.
@param consumer: Any L{IConsumer} provider
"""
self._task = self._cooperate(self._writeloop(consumer))
d = self._task.whenDone()
def maybeStopped(reason):
# IBodyProducer.startProducing's Deferred isn't support to fire if
# stopProducing is called.
reason.trap(task.TaskStopped)
return defer.Deferred()
d.addCallbacks(lambda ignored: None, maybeStopped)
return d
示例2: test_sendRequestBodyWithError
# 需要导入模块: from twisted.web import iweb [as 别名]
# 或者: from twisted.web.iweb import IBodyProducer [as 别名]
def test_sendRequestBodyWithError(self):
"""
If the L{Deferred} returned from the C{startProducing} method of the
L{IBodyProducer} passed to L{Request} fires with a L{Failure}, the
L{Deferred} returned from L{Request.writeTo} fails with that
L{Failure}.
"""
producer = StringProducer(5)
request = Request(b'POST', b'/bar', _boringHeaders, producer)
writeDeferred = request.writeTo(self.transport)
# Sanity check - the producer should be registered with the underlying
# transport.
self.assertIdentical(self.transport.producer, producer)
self.assertTrue(self.transport.streaming)
producer.consumer.write(b'ab')
self.assertEqual(
self.transport.value(),
b"POST /bar HTTP/1.1\r\n"
b"Connection: close\r\n"
b"Content-Length: 5\r\n"
b"Host: example.com\r\n"
b"\r\n"
b"ab")
self.assertFalse(self.transport.disconnecting)
producer.finished.errback(Failure(ArbitraryException()))
# Disconnection is handled by a higher level. Request should leave the
# transport alone in this case.
self.assertFalse(self.transport.disconnecting)
# Oh. Except it should unregister the producer that it registered.
self.assertIdentical(self.transport.producer, None)
return self.assertFailure(writeDeferred, ArbitraryException)
示例3: test_sendRequestBodyWithError
# 需要导入模块: from twisted.web import iweb [as 别名]
# 或者: from twisted.web.iweb import IBodyProducer [as 别名]
def test_sendRequestBodyWithError(self):
"""
If the L{Deferred} returned from the C{startProducing} method of the
L{IBodyProducer} passed to L{Request} fires with a L{Failure}, the
L{Deferred} returned from L{Request.writeTo} fails with that
L{Failure}.
"""
producer = StringProducer(5)
request = Request('POST', '/bar', _boringHeaders, producer)
writeDeferred = request.writeTo(self.transport)
# Sanity check - the producer should be registered with the underlying
# transport.
self.assertIdentical(self.transport.producer, producer)
self.assertTrue(self.transport.streaming)
producer.consumer.write('ab')
self.assertEqual(
self.transport.value(),
"POST /bar HTTP/1.1\r\n"
"Connection: close\r\n"
"Content-Length: 5\r\n"
"Host: example.com\r\n"
"\r\n"
"ab")
self.assertFalse(self.transport.disconnecting)
producer.finished.errback(Failure(ArbitraryException()))
# Disconnection is handled by a higher level. Request should leave the
# transport alone in this case.
self.assertFalse(self.transport.disconnecting)
# Oh. Except it should unregister the producer that it registered.
self.assertIdentical(self.transport.producer, None)
return self.assertFailure(writeDeferred, ArbitraryException)