MCPcopy
hub / github.com/aio-libs/aiohttp / write_bytes

Method write_bytes

aiohttp/client_reqrep.py:369–466  ·  view source on GitHub ↗

Support coroutines that yields bytes objects.

(self, request, reader)

Source from the content-addressed store, hash-verified

367
368 @asyncio.coroutine
369 def write_bytes(self, request, reader):
370 """Support coroutines that yields bytes objects."""
371 # 100 response
372 if self._continue is not None:
373 yield from self._continue
374
375 try:
376 if asyncio.iscoroutine(self.body):
377 request.transport.set_tcp_nodelay(True)
378 exc = None
379 value = None
380 stream = self.body
381
382 while True:
383 try:
384 if exc is not None:
385 result = stream.throw(exc)
386 else:
387 result = stream.send(value)
388 except StopIteration as exc:
389 if isinstance(exc.value, bytes):
390 yield from request.write(exc.value, drain=True)
391 break
392 except:
393 self.response.close()
394 raise
395
396 if isinstance(result, asyncio.Future):
397 exc = None
398 value = None
399 try:
400 value = yield result
401 except Exception as err:
402 exc = err
403 elif isinstance(result, (bytes, bytearray)):
404 yield from request.write(result, drain=True)
405 value = None
406 else:
407 raise ValueError(
408 'Bytes object is expected, got: %s.' %
409 type(result))
410
411 elif isinstance(self.body, asyncio.StreamReader):
412 request.transport.set_tcp_nodelay(True)
413 chunk = yield from self.body.read(streams.DEFAULT_LIMIT)
414 while chunk:
415 yield from request.write(chunk, drain=True)
416 chunk = yield from self.body.read(streams.DEFAULT_LIMIT)
417
418 elif isinstance(self.body, streams.DataQueue):
419 request.transport.set_tcp_nodelay(True)
420 while True:
421 try:
422 chunk = yield from self.body.read()
423 if chunk is EOF_MARKER:
424 break
425 yield from request.write(chunk, drain=True)
426 except streams.EofStream:

Callers 1

sendMethod · 0.95

Calls 8

throwMethod · 0.80
set_tcp_nodelayMethod · 0.45
sendMethod · 0.45
writeMethod · 0.45
closeMethod · 0.45
readMethod · 0.45
set_exceptionMethod · 0.45
write_eofMethod · 0.45

Tested by

no test coverage detected