NCP client¶
ncplib
allows you to connect to a NCP server and issue commands.
Overview¶
Connecting to a NCP server¶
Connect to a NCP server using connect()
. The returned Connection
will automatically close when the
connection block exits.
import ncplib
async with await ncplib.connect("127.0.0.1", 9999) as connection:
pass # Your client code here.
# Connection is automatically closed here.
Sending a packet¶
Send a NCP packet containing a single NCP field using Connection.send()
:
response = connection.send("DSPC", "TIME", SAMP=1024, FCTR=1200)
Receiving replies to a packet¶
The return value of Connection.send()
is a Response
. Receive a single NCP field reply using
Response.recv()
:
field = await response.recv()
Alternatively, use the Response
as an async iterator to loop over multiple NCP field replies:
async for field in response:
pass
Important
The async for loop will only terminate when the underlying connection closes.
Accessing field data¶
The return value of Response.recv()
is a Field
, representing a NCP field. Access contained
NCP parameters using item access:
print(field["TSDC"])
Advanced usage¶
API reference¶
-
ncplib.
connect
(host: str, port: Optional[int] = None, *, remote_hostname: Optional[str] = None, hostname: Optional[str] = None, connection_username: Optional[str] = None, connection_domain: str = '', timeout: int = 60, auto_erro: bool = True, auto_warn: bool = True, auto_ackn: bool = True, ssl: bool | ssl.SSLContext = False, username: str = '', password: str = '') → Connection¶ Connects to a NCP server.
Parameters: - host (str) – The hostname of the NCP server. This can be an IP address or domain name.
- port (int) – The port number of the NCP server.
- remote_hostname (str) – The identifying hostname for the remote end of the connection. If omitted, this will be the host:port of the NCP server.
- hostname (str) – The identifying hostname in the client connection. Defaults to the system hostname.
- connection_username (str) – The identifying username in the client connection. Defaults to the login name of the system user.
- connection_domain (str) – The identifying domain in the client connection.
- timeout (int) – The network timeout (in seconds). Applies to: connecting, receiving a packet, closing connection.
- auto_erro (bool) – Automatically raise a
CommandError
on receiving anERRO
NCP parameter. - auto_warn (bool) – Automatically issue a
CommandWarning
on receiving aWARN
NCP parameter. - auto_ackn (bool) – Automatically ignore NCP fields containing an
ACKN
NCP parameter. - ssl (bool) – Connect to the Node using an encrypted (TLS) connection. Requires TLS support on the Node.
- username (str) – Authenticate with the Node using the given username. Requires authentication support on the Node.
- password (str) – Authenticate with the Node using the given password. Requires authentication support on the Node.
Raises: ncplib.NCPError – if the NCP connection failed.
Returns: The client
Connection
.Return type: