本文整理汇总了Python中asyncio.IncompleteReadError方法的典型用法代码示例。如果您正苦于以下问题:Python asyncio.IncompleteReadError方法的具体用法?Python asyncio.IncompleteReadError怎么用?Python asyncio.IncompleteReadError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asyncio
的用法示例。
在下文中一共展示了asyncio.IncompleteReadError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __read_message_frame
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def __read_message_frame(self):
length_bytes = await self.__reader.read(n=4)
if len(length_bytes) == 0:
raise asyncio.IncompleteReadError(length_bytes, 4)
length = Util.int_from_bytes(length_bytes)
operation = Util.int_from_bytes(await self.__reader.read(n=1))
# Read from the socket until we have read the full packet
payload = bytearray()
read_bytes = 1
while read_bytes < length:
next_payload = await self.__reader.read(n=length - read_bytes)
if len(next_payload) == 0:
raise asyncio.IncompleteReadError(next_payload, length - read_bytes)
payload.extend(next_payload)
read_bytes += len(next_payload)
try:
op = Operation.from_dict(operation)
except ValueError:
self.logger.error("Found unknown operation %d", operation)
op = Operation.Unknown
return MessageFrame(operation=op, payload=bytes(payload))
示例2: connect_reader_to_writer
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def connect_reader_to_writer(reader, writer):
BUF_SIZE = 8192
try:
while True:
data = await reader.read(BUF_SIZE)
if not data:
if not writer.transport.is_closing():
writer.write_eof()
await writer.drain()
return
writer.write(data)
await writer.drain()
except (OSError, asyncio.IncompleteReadError) as e:
pass
示例3: tg_connect_reader_to_writer
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def tg_connect_reader_to_writer(rd, wr, user, rd_buf_size, is_upstream):
try:
while True:
data = await rd.read(rd_buf_size)
if isinstance(data, tuple):
data, extra = data
else:
extra = {}
if not data:
wr.write_eof()
await wr.drain()
return
else:
if is_upstream:
update_user_stats(user, octets_from_client=len(data), msgs_from_client=1)
else:
update_user_stats(user, octets_to_client=len(data), msgs_to_client=1)
wr.write(data, extra)
await wr.drain()
except (OSError, asyncio.IncompleteReadError) as e:
# print_err(e)
pass
示例4: test_readexactly_eof
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def test_readexactly_eof(self):
# Read exact number of bytes (eof).
stream = asyncio.StreamReader(loop=self.loop)
n = 2 * len(self.DATA)
read_task = asyncio.Task(stream.readexactly(n), loop=self.loop)
def cb():
stream.feed_data(self.DATA)
stream.feed_eof()
self.loop.call_soon(cb)
with self.assertRaises(asyncio.IncompleteReadError) as cm:
self.loop.run_until_complete(read_task)
self.assertEqual(cm.exception.partial, self.DATA)
self.assertEqual(cm.exception.expected, n)
self.assertEqual(str(cm.exception),
'18 bytes read on a total of 36 expected bytes')
self.assertEqual(b'', stream._buffer)
示例5: run
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def run(self):
await self._client_connected()
while not self.__writer.is_closing():
try:
data = await self.__reader.readuntil(
separator=Spheniscidae.Delimiter)
if data:
await self.__data_received(data)
else:
self.__writer.close()
await self.__writer.drain()
except IncompleteReadError:
self.__writer.close()
except CancelledError:
self.__writer.close()
except ConnectionResetError:
self.__writer.close()
except LimitOverrunError:
self.__writer.close()
except BaseException as e:
self.logger.exception(e.__traceback__)
await self._client_disconnected()
示例6: fetch
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def fetch(self):
while True:
try:
hdrlen = constants.STREAM_HEADER_SIZE_BYTES
header = yield from self._response.content.readexactly(hdrlen)
_, length = struct.unpack(">BxxxL", header)
if not length:
continue
data = yield from self._response.content.readexactly(length)
except (
aiohttp.ClientConnectionError,
aiohttp.ServerDisconnectedError,
asyncio.IncompleteReadError,
):
break
return data
示例7: _handle_packets
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def _handle_packets(self):
data = True
while data:
try:
data = await self._reader.readuntil(b"}}")
if not data:
break
message = data.decode("ascii")
await self.parse_api_messages(message)
except (asyncio.IncompleteReadError, TimeoutError, ConnectionResetError, OSError) as e:
_LOGGER.error(
"pyIntesisHome lost connection to the %s server. Exception: %s", self._device_type, e
)
break
self._connected = False
self._connecting = False
self._authToken = None
self._reader = None
self._writer = None
self._sendQueueTask.cancel()
await self._send_update_callback()
return
示例8: connect
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def connect(self):
loop = asyncio.get_event_loop()
while True:
try:
self.reader, self.writer = await asyncio.open_connection(
self.server.config['masterserver_ip'],
self.server.config['masterserver_port'],
loop=loop)
await self.handle_connection()
except (ConnectionRefusedError, TimeoutError,
ConnectionResetError, asyncio.IncompleteReadError):
logger.debug('Connection error occurred.')
self.writer = None
self.reader = None
finally:
logger.debug('Retrying MS connection in 30 seconds.')
await asyncio.sleep(30)
示例9: __call__
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def __call__(self, reader, writer):
if self.framed:
reader = TFramedTransport(reader)
writer = TFramedTransport(writer)
iproto = self.protocol_cls(reader)
oproto = self.protocol_cls(writer)
while not reader.at_eof():
try:
with async_timeout.timeout(self.timeout):
await self.processor.process(iproto, oproto)
except ConnectionError:
logger.debug("client has closed the connection")
writer.close()
except asyncio.TimeoutError:
logger.debug("timeout when processing the client request")
writer.close()
except asyncio.IncompleteReadError:
logger.debug("client has closed the connection")
writer.close()
except Exception:
# app exception
logger.exception("unhandled app exception")
writer.close()
writer.close()
示例10: test_streaming_with_timeout__expects_timeout
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def test_streaming_with_timeout__expects_timeout(self):
app = Vibora()
async def stream():
for _ in range(0, 100):
await asyncio.sleep(2)
yield b'1'
@app.route('/')
async def home():
return StreamingResponse(stream, complete_timeout=1)
async with app.test_client() as client:
try:
await client.get('/', timeout=3)
self.fail('Vibora should have closed the connection because a streaming timeout is not recoverable.')
except asyncio.IncompleteReadError:
pass
except futures.TimeoutError:
pass
示例11: _recv_loop
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def _recv_loop(self):
"""
This loop is constantly putting items on the queue as they're read.
"""
while self._connected:
try:
data = await self._recv()
except asyncio.CancelledError:
break
except Exception as e:
if isinstance(e, (IOError, asyncio.IncompleteReadError)):
msg = 'The server closed the connection'
self._log.info(msg)
elif isinstance(e, InvalidChecksumError):
msg = 'The server response had an invalid checksum'
self._log.info(msg)
else:
msg = 'Unexpected exception in the receive loop'
self._log.exception(msg)
await self.disconnect()
# Add a sentinel value to unstuck recv
if self._recv_queue.empty():
self._recv_queue.put_nowait(None)
break
try:
await self._recv_queue.put(data)
except asyncio.CancelledError:
break
示例12: read_packet
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def read_packet(self, reader):
while True:
line = await reader.readline()
if not line or line[-1] != b'\n':
raise asyncio.IncompleteReadError(line, None)
if line.lower().startswith(b'content-length: '):
await reader.readexactly(2)
length = int(line[16:-2])
return await reader.readexactly(length)
示例13: __read_loop
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def __read_loop(self):
# Continually try to read packets from the socket
while not self.__closed:
try:
try:
self.__log_trace("Starting long poll read")
response = await self.__read_message_frame()
self.__log_trace("Got message frame from server: %s", response)
except asyncio.IncompleteReadError:
if self.__closed:
return
self.logger.error("Unable to read from socket, likely socket is closed or server died")
self.connected = False
try:
await self.__connect()
except ConnectionError:
# Already logged in __connect, so just ignore it here
pass
except ConnectFailedException:
# Already logged in __connect_request_response, so just ignore it here
pass
return
payload = cbor2.loads(response.payload)
await self.__handle_read_response(payload, response)
except Exception:
self.logger.exception("Unhandled exception occurred")
return
示例14: __connect_request_response
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def __connect_request_response(self):
data = ConnectRequest()
data.request_id = Util.get_request_id()
data.sdk_version = self.__SDK_VERSION
data.protocol_version = self.__PROTOCOL_VERSION
if self.auth_token is not None:
data.auth_token = self.auth_token
# Write the connect version
self.__writer.write(Util.int_to_bytes(self.__CONNECT_VERSION, 1))
# Write request to socket
frame = MessageFrame(operation=Operation.Connect, payload=cbor2.dumps(data.as_dict()))
self.__writer.write(Util.encode_frame(frame))
await self.__writer.drain()
# Read connect version
connect_response_version_byte = await self.__reader.read(n=1)
if len(connect_response_version_byte) == 0:
raise asyncio.IncompleteReadError(connect_response_version_byte, 1)
connect_response_version = Util.int_from_bytes(connect_response_version_byte)
if connect_response_version != self.__CONNECT_VERSION:
self.logger.error("Unexpected response from the server, Connect version: %s.", connect_response_version)
raise ConnectFailedException("Failed to establish connection with the server")
# Read connect response
response = await self.__read_message_frame() # type: MessageFrame
if response.operation == Operation.ConnectResponse:
payload = cbor2.loads(response.payload)
response = ConnectResponse.from_dict(payload) # type: ConnectResponse
self.logger.debug("Received ConnectResponse from server: %s", response)
else:
self.logger.error("Received data with unexpected operation %s.", response.operation)
raise ConnectFailedException("Failed to establish connection with the server")
if response.status != ResponseStatusCode.Success:
self.logger.error("Received ConnectResponse with unexpected status %s.", response.status)
raise ConnectFailedException("Failed to establish connection with the server")
示例15: read_response
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import IncompleteReadError [as 别名]
def read_response(self, initial_connect=False):
try:
raw_size = await self.reader.readexactly(size_struct.size)
except asyncio.IncompleteReadError:
raise ConnectionAbortedError
size = size_struct.unpack(raw_size)[0]
# connect and close op replies don't contain a reply header
if initial_connect or self.pending_specials[protocol.CLOSE_XID]:
raw_payload = await self._read(size)
response = protocol.ConnectResponse.deserialize(raw_payload)
return (None, None, response)
raw_header = await self._read(reply_header_struct.size)
xid, zxid, error_code = reply_header_struct.unpack_from(raw_header)
if error_code:
self.opcode_xref.pop(xid)
return (xid, zxid, exc.get_response_error(error_code))
size -= reply_header_struct.size
raw_payload = await self._read(size)
if xid == protocol.WATCH_XID:
response = protocol.WatchEvent.deserialize(raw_payload)
else:
opcode = self.opcode_xref.pop(xid)
response = protocol.response_xref[opcode].deserialize(raw_payload)
return (xid, zxid, response)