Network

The network subpackage provides low-level base components for networking clients.

The base classes can be inherited to assist with writing adapters for integrations with exchanges, brokerages, data providers/feeds and other venues or services.

HTTP Client

class HttpClient ( loop: asyncio.AbstractEventLoop , Logger logger , list addresses=None , list nameservers=None , int ttl_dns_cache=ONE_DAY , ssl: Union[None , bool , Fingerprint , SSLContext] = False , dict connector_kwargs=None )

Bases: object

Provides an asynchronous HTTP client.

Parameters
  • loop ( asyncio.AbstractEventLoop ) – The event loop for the client.

  • logger ( Logger ) – The logger for the client.

  • ttl_dns_cache ( int ) – The time to live for the DNS cache.

  • ssl ( Union [ None , bool , Fingerprint , SSLContext ] , default False ) – The ssl context to use for HTTPS.

  • connector_kwargs ( dict , optional ) – The connector key word arguments.

Raises

ValueError – If ttl_dns_cache is not positive (> 0).

connect ( self ) None

Connect the HTTP client session.

connected

Return whether the HTTP client is connected.

Returns

bool

delete ( self , unicode url: str , headers: Optional[Dict[str , str]] = None , **kwargs ) ClientResponse
disconnect ( self ) None

Disconnect the HTTP client session.

get ( self , unicode url: str , headers: Optional[Dict[str , str]] = None , **kwargs ) ClientResponse
post ( self , unicode url: str , headers: Optional[Dict[str , str]] = None , **kwargs ) ClientResponse
request ( self , unicode method: str , unicode url: str , headers: Optional[Dict[str , str]] = None , json: Optional[Dict[str , Any]] = None , **kwargs ) ClientResponse
session

Return the current HTTP client session.

Returns

aiohttp.ClientSession

Socket

class SocketClient ( loop: asyncio.AbstractEventLoop , Logger logger: Logger , host , port , handler: Callable , bool ssl=True , bytes crlf=None , unicode encoding=u'utf-8' )

Bases: object

Provides a low-level generic socket base client.

Parameters
  • loop ( asyncio.AbstractEventLoop ) – The event loop for the client.

  • logger ( Logger ) – The logger for the client.

  • host ( str ) – The host for the client.

  • port ( int ) – The port for the client.

  • handler ( Callable ) – The handler to process the raw bytes read.

  • ssl ( bool ) – If SSL should be used for socket connection.

  • crlf ( bytes , optional ) – The carriage return, line feed delimiter on which to split messages.

  • encoding ( str , optional ) – The encoding to use when sending messages.

Raises
  • ValueError – If host is not a valid string.

  • ValueError – If port is not positive (> 0).

connect ( self )
disconnect ( self )
host

The host for the socket client.

Returns

str

is_connected

If the socket is connected.

Returns

bool

port

The port for the socket client.

Returns

int

post_connection ( self )

The actions to perform post-connection. i.e. sending further connection messages.

reconnect ( self )
send ( self , bytes raw )
ssl

If the socket client is using SSL.

Returns

bool

start ( self )
stop ( self )

WebSocket Client

class WebSocketClient ( loop: asyncio.AbstractEventLoop, Logger logger: Logger, handler: Callable[[bytes], None], int max_retry_connection=0, bytes pong_msg=None, bool log_send=False, bool log_recv=False )

Bases: object

Provides a low-level web socket base client.

Parameters
  • loop ( asyncio.AbstractEventLoop ) – The event loop for the client.

  • logger ( LoggerAdapter ) – The logger adapter for the client.

  • handler ( Callable [ [ bytes ] , None ] ) – The handler for receiving raw data.

  • max_retry_connection ( int , default 0 ) – The number of times to attempt a reconnection.

  • pong_msg ( bytes , optional ) – The pong message expected from the server (used to filter).

  • log_send ( bool , default False ) – If the raw sent bytes for each message should be logged.

  • log_recv ( bool , default False ) – If the raw recv bytes for each message should be logged.

close ( self )
connect ( self , unicode ws_url , bool start=True , **ws_kwargs ) None

Connect the WebSocket client.

Will call post_connection() prior to starting receive loop.

Parameters
  • ws_url ( str ) – The endpoint URL to connect to.

  • start ( bool , default True ) – If the WebSocket should start its receive loop.

  • ws_kwargs ( dict ) – The optional kwargs for connection.

Raises

ValueError – If ws_url is not a valid string.

connection_retry_count

The current connection retry count.

Returns

int

disconnect ( self ) None

Disconnect the WebSocket client session.

Will call post_disconnection() .

is_connected

If the client is connected.

Returns

bool

max_retry_connection

The max connection retries.

Returns

int

post_connection ( self ) None

Actions to be performed post connection.

post_disconnection ( self ) None

Actions to be performed post disconnection.

post_reconnection ( self ) None

Actions to be performed post reconnection.

receive ( self ) Optional [ bytes ]
reconnect ( self ) None

Reconnect the WebSocket client session.

Will call post_reconnection() following connection.

send ( self , bytes raw ) None
send_json ( self , dict msg ) None
start ( self ) None
unknown_message_count

The current unknown message count.

Returns

int