duck.utils.xsocket.io¶
Socket I/O implementations.
Module Contents¶
Classes¶
Class for doing socket I/O operations like connect, send and receive data through the network. |
Functions¶
Decorator for checking if socket is an instance of xsocket otherwise an error is raised. |
Data¶
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
chunkedTransfer-Encoding or request doesn’t haveContent-Lengthheader 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
chunkedTransfer-Encoding or request doesn’t haveContent-Lengthheader 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:
ExceptionRaised 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.