duck.utils.xsocket.ioยถ

Socket I/O implementations.

Module Contentsยถ

Classesยถ

SocketIO

Class for doing socket I/O operations like connect, send and receive data through the network.

Functionsยถ

check_socket

Decorator for checking if socket is an instance of xsocket otherwise an error is raised.

Dataยถ

CONTENT_LENGTH_PATTERN

REQUEST_TIMEOUT

SEND_TIMEOUT

SERVER_BUFFER

STREAM_TIMEOUT

TRANSFER_ENCODING_PATTERN

APIยถ

duck.utils.xsocket.io.CONTENT_LENGTH_PATTERNยถ

โ€˜compile(โ€ฆ)โ€™

duck.utils.xsocket.io.REQUEST_TIMEOUTยถ

None

duck.utils.xsocket.io.SEND_TIMEOUTยถ

None

duck.utils.xsocket.io.SERVER_BUFFERยถ

None

duck.utils.xsocket.io.STREAM_TIMEOUTยถ

None

class duck.utils.xsocket.io.SocketIOยถ

Class for doing socket I/O operations like connect, send and receive data through the network.

async classmethod async_connect(sock: duck.utils.xsocket.xsocket, target: Tuple[str, int], timeout: float = None)ยถ

Asynchronously connect to a target.

async classmethod async_receive(sock: duck.utils.xsocket.xsocket, timeout: float = REQUEST_TIMEOUT, bufsize: int = SERVER_BUFFER) โ†’ bytesยถ

Asynchronously receive data from the socket.

Parameters:
  • sock โ€“ The xsocket object to receive data from.

  • timeout โ€“ The timeout in seconds to receive data. Defaults to REQUEST_TIMEOUT set in settings.py.

  • bufsize โ€“ Max number of bytes to read.

Raises:

TimeoutError โ€“ If no data is received within the specified time.

Returns:

The received data in bytes.

Return type:

bytes

async classmethod async_receive_full_request(sock: duck.utils.xsocket.xsocket, timeout: float = REQUEST_TIMEOUT, stream_timeout: float = STREAM_TIMEOUT) โ†’ bytesยถ

Asynchronously receives the complete request data from the socket.

Parameters:
  • sock โ€“ The underlying xsocket object

  • timeout โ€“ Timeout in seconds to receive the first part of the data. Defaults to REQUEST_TIMEOUT set in settings.py.

  • stream_timeout โ€“ The timeout in seconds to receive the next stream of bytes after the first part has been received. This is only used if request is using chunked Transfer-Encoding or request doesnโ€™t have Content-Length header set.

Raises:
  • TimeoutError โ€“ If no data is received within the first timeout (not stream timeout).

  • Exception โ€“ Any other exception, e.g. connection errors.

Returns:

The received data in bytes.

Return type:

bytes

async classmethod async_send(sock: duck.utils.xsocket.xsocket, data: bytes, timeout: float = SEND_TIMEOUT, suppress_errors: bool = False, ignore_error_list: List[Type[Exception]] = [ssl.SSLError, BrokenPipeError, OSError]) โ†’ intยถ

Asynchronously sends raw data directly to a client socket.

Parameters:
  • sock โ€“ The client xsocket object that will receive the data.

  • data โ€“ The data to be sent in bytes.

  • timeout โ€“ Timeout for sending data.

  • suppress_errors โ€“ If True, suppresses any errors (errors not in ignore_error_list) that occur during the sending process. Defaults to False.

  • ignore_error_list โ€“ List of error classes to ignore when raised during data transfer.

Returns:

The number of bytes that has been sent (useful if suppress_errors=True)

Return type:

int

Raises:
  • BrokenPipeError โ€“ If the connection is broken during data transmission.

  • Exception โ€“ Any other exceptions that occur during the sending process.

classmethod close(sock: duck.utils.xsocket.xsocket, shutdown: bool = True, shutdown_reason: int = socket.SHUT_RDWR, ignore_xsocket_error: bool = False)ยถ

Closes a socket.

Parameters:
  • sock โ€“ The underlying xsocket object.

  • shutdown โ€“ Whether to shutdown the socket using sock.shutdown.

  • shutdown_reason โ€“ Reason for shutdown.

  • ignore_xsocket_error โ€“ Whether to ignore xsocket error when closing socket.

classmethod connect(sock: duck.utils.xsocket.xsocket, target: Tuple[str, int], timeout: float = None)ยถ

Connect to a target.

classmethod receive(sock: duck.utils.xsocket.xsocket, timeout: float = REQUEST_TIMEOUT, bufsize: int = SERVER_BUFFER) โ†’ bytesยถ

Receive data from the socket.

Parameters:
  • sock โ€“ The xsocket object to receive data from.

  • timeout โ€“ The timeout in seconds to receive data. Defaults to REQUEST_TIMEOUT set in settings.py.

  • bufsize โ€“ Max number of bytes to read.

Raises:

TimeoutError โ€“ If no data is received within the specified time.

Returns:

The received data in bytes.

Return type:

bytes

classmethod receive_full_request(sock: duck.utils.xsocket.xsocket, timeout: float = REQUEST_TIMEOUT, stream_timeout: float = STREAM_TIMEOUT) โ†’ bytesยถ

Receives the complete request data from the socket.

Parameters:
  • sock โ€“ The underlying xsocket object

  • timeout โ€“ Timeout in seconds to receive the first part of the data. Defaults to REQUEST_TIMEOUT set in settings.py.

  • stream_timeout โ€“ The timeout in seconds to receive the next stream of bytes after the first part has been received. This is only used if request is using chunked Transfer-Encoding or request doesnโ€™t have Content-Length header set.

Raises:
  • TimeoutError โ€“ If no data is received within the first timeout (not stream timeout).

  • Exception โ€“ Any other exception, e.g. connection errors.

Returns:

The received data in bytes.

Return type:

bytes

classmethod send(sock: duck.utils.xsocket.xsocket, data: bytes, timeout: float = SEND_TIMEOUT, suppress_errors: bool = False, ignore_error_list: List[Type[Exception]] = [ssl.SSLError, BrokenPipeError, OSError, ConnectionError]) โ†’ intยถ

Sends raw data directly to a client socket.

Parameters:
  • sock โ€“ The client xsocket object that will receive the data.

  • data โ€“ The data to be sent in bytes.

  • timeout โ€“ Timeout for sending data.

  • suppress_errors โ€“ If True, suppresses any errors (errors not in ignore_error_list) that occur during the sending process. Defaults to False.

  • ignore_error_list โ€“ List of error classes to ignore when raised during data transfer.

Returns:

The number of bytes that has been sent (useful if suppress_errors=True)

Return type:

int

Raises:
  • BrokenPipeError โ€“ If the connection is broken during data transmission.

  • Exception โ€“ Any other exceptions that occur during the sending process.

exception duck.utils.xsocket.io.SocketIOErrorยถ

Bases: Exception

Raised on socket I/O errors.

Initialization

Initialize self. See help(type(self)) for accurate signature.

duck.utils.xsocket.io.TRANSFER_ENCODING_PATTERNยถ

โ€˜compile(โ€ฆ)โ€™

duck.utils.xsocket.io.check_socket(func)ยถ

Decorator for checking if socket is an instance of xsocket otherwise an error is raised.