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.