File: _stream.py

package info (click to toggle)
python-socks 2.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: python: 5,195; sh: 8; makefile: 3
file content (32 lines) | stat: -rw-r--r-- 855 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import curio.io
import curio.socket

from ... import _abc as abc
from ..._errors import ProxyError

DEFAULT_RECEIVE_SIZE = 65536


class CurioSocketStream(abc.AsyncSocketStream):
    _socket: curio.io.Socket = None

    def __init__(self, sock: curio.io.Socket):
        self._socket = sock

    async def write_all(self, data):
        await self._socket.sendall(data)

    async def read(self, max_bytes=DEFAULT_RECEIVE_SIZE):
        return await self._socket.recv(max_bytes)

    async def read_exact(self, n):
        data = bytearray()
        while len(data) < n:
            packet = await self._socket.recv(n - len(data))
            if not packet:  # pragma: no cover
                raise ProxyError('Connection closed unexpectedly')
            data += packet
        return data

    async def close(self):
        await self._socket.close()