Servers

class nclib.server.TCPServer(bindto: Tuple[str, int], kernel_backlog: int = 5, **kwargs)[source]

A simple TCP server model. Iterating over it will yield client sockets as Netcat objects.

Parameters:
  • bindto – The address to bind to, a tuple (host, port)

  • kernel_backlog – The argument to listen()

Any additional keyword arguments will be passed to the constructor of the Netcat object that is constructed for each client.

Here is a simple echo server example:

>>> from nclib import TCPServer
>>> server = TCPServer(('0.0.0.0', 1337))
>>> for client in server:
...     client.send(client.recv()) # or submit to a thread pool for async handling...
__init__(bindto: Tuple[str, int], kernel_backlog: int = 5, **kwargs)[source]
close()[source]

Tear down this server and release its resources

class nclib.server.UDPServer(bindto, dgram_size=4096)[source]

A simple UDP server model. Iterating over it will yield of tuples of datagrams and peer addresses. To respond, use the respond method, which takes the response and the peer address.

Parameters:
  • bindto – The address to bind to, a tuple (host, port)

  • dgram_size – The size of the datagram to receive. This is important! If you send a message longer than the receiver’s receiving size, the rest of the message will be silently lost! Default is 4096.

Here is a simple echo server example:

>>> from nclib import UDPServer
>>> server = UDPServer(('0.0.0.0', 1337))
>>> for message, peer in server:
...     server.respond(message, peer) # or submit to a thread pool for async handling...
__init__(bindto, dgram_size=4096)[source]
close()[source]

Tear down this server and release its resources

respond(packet, peer, flags=0)[source]

Send a message back to a peer.

Parameters:
  • packet – The data to send

  • peer – The address to send to, as a tuple (host, port)

  • flags – Any sending flags you want to use for some reason